Anda di halaman 1dari 23

DB TECH

ASSIGNMENT 7
EXERCISE 5
Stefan Lederer | 0761467
Exercise 5
 Use the given source code below for real
tests and identification of performance
bottlenecks. Write a small test program
and call update within loops several
times.
Questions:
 Provide your test cases and also some
performance measures.
 Identify bottlenecks and fix them.
 Compare your solution(s) to the given
one.
Performance measures

long milliStart;
long milliEnd;

milliStart = System.currentTimeMillis();

version1(dbm, sql, dburl, user, passwd);

milliEnd = System.currentTimeMillis();

System.out.println("Used time: " + (milliEnd -


milliStart) + " milli-sec");
Given Code

for(int i = 0; i<NUM_LOOPS; i++){


System.out.println("Execution Query " + (i+1) + "...");
exec(sql, dburl, user, passwd);
}

protected void exec(String sqlStatement, String dburl, String use


try{
Connection con = DriverManager.getConnection(dburl,
Statement s = con.createStatement();
s.execute(sqlStatement);
s.close();
con.close();

}catch(Exception e){
System.err.println("Error: " + e.getMessage());
}
}
Queries
 Select
 Select
* from country where population > 0
and population < 50 000
 Update
 updatecountry set area = 80000,
population = 8000000 where code = 'A'
Update
 UPDATE TESTS
 __________________________
 Starting...
 Execution Query 1...
 Execution Query 2...
 Execution Query 3...
 Execution Query 4...
 Execution Query 5...
 Execution Query 6...
 Execution Query 7...
 Execution Query 8...
 Execution Query 9...
 Execution Query 10...
 Used time: 14102 milli-sec
Select
 SELECT TESTS
 __________________________
 Starting...
 Execution Query 1...
 Execution Query 2...
 Execution Query 3...
 Execution Query 4...
 Execution Query 5...
 Execution Query 6...
 Execution Query 7...
 Execution Query 8...
 Execution Query 9...
 Execution Query 10...
 Used time: 13564 milli-sec
Bottlenecks
 Creating/Close the connection each time
 Create
the connection at the begining,
update the stuff and close it afterwards
 No use of prepared Statements
 Prepared statements are faster
 It is given an SQL statement when it is
created
 This SQL statement is sent to the DBMS
right away, where it is compiled
 Use of SQL statement that has been
precompiled
Connection at beginning
try{
Connection con = DriverManager.getConnection(dburl, user,
for(int i = 0; i<NUM_LOOPS; i++){
System.out.println("Execution Query " + (i+1) + "...
exec(sql, con);
}
con.close();
}catch(Exception e){
System.err.println("Error: " + e.getMessage());
}

protected void exec(String sqlStatement,Connection con) throws


Exception{
Statement s = con.createStatement();
s.execute(sqlStatement);
s.close();
}
Update
 Starting...
 Execution Query 1...
 Execution Query 2...
 Execution Query 3...
 Execution Query 4...
 Execution Query 5...
 Execution Query 6...
 Execution Query 7...
 Execution Query 8...
 Execution Query 9...
 Execution Query 10...
 Used time: 2075 milli-sec
Select
 Starting...
 Execution Query 1...
 Execution Query 2...
 Execution Query 3...
 Execution Query 4...
 Execution Query 5...
 Execution Query 6...
 Execution Query 7...
 Execution Query 8...
 Execution Query 9...
 Execution Query 10...
 Used time: 2698 milli-sec
Prepared Statements
try{
Connection con = DriverManager.getConnection(dburl, user, p
PreparedStatement stmt = con.prepareStatement(sql);
for(int i = 0; i<NUM_LOOPS; i++){
System.out.println("Execution Query " + (i+1) + "..."
exec(80000 + i * 1000, 8000000 + i * 10000,stmt);
}
stmt.close();
con.close();
}catch(Exception e){
System.err.println("Error: " + e.getMessage());
}

protected void exec(int p1, int p2, PreparedStatement stmt) throws


stmt.setInt(1, p1);
stmt.setInt(2, p2);
stmt.execute();
}
Statements
 Select
 Select
* from country where population > ?
and population < ?
 Update
 updatecountry set area = ?, population = ?
where code = 'A'
Update
 Starting...
 Execution Query 1...
 Execution Query 2...
 Execution Query 3...
 Execution Query 4...
 Execution Query 5...
 Execution Query 6...
 Execution Query 7...
 Execution Query 8...
 Execution Query 9...
 Execution Query 10...
 Used time: 1565 milli-sec
Select
 Starting...
 Execution Query 1...
 Execution Query 2...
 Execution Query 3...
 Execution Query 4...
 Execution Query 5...
 Execution Query 6...
 Execution Query 7...
 Execution Query 8...
 Execution Query 9...
 Execution Query 10...
 Used time: 1689 milli-sec
Further possibilities
 Getting the connection and logging in is
the part that takes the most time
 An application can easily spend several
seconds every time it needs to establish a
connection.
 Solution: Caching Connection in
Connection Pools
 Cached connections are kept in a runtime
object pool
 They can be used and reused as needed by
the application

Connection Pooling
 Connection pooling in the JDBC 2.0
extension API is a framework for caching
database connection
 Allows reuse of physical connections
 reduced overhead for your application
 minimizes expensive operations in the
creation and closing of sessions
Connection Pooling –
Concepts
 Connection pool data sources
 return pooled connection instances

 Pooled connections
 instance represents a single physical
connection to a database
 remaining open during use by a series of
logical connection instances
 Logical Connection
 It‘s a simple connection instance returned by a
pooled connection instance
 logical connection instance acts as a temporary
handle to the physical connection
Connection Pooling - Code
OracleConnectionPoolDataSource ocpds = new

OracleConnectionPoolDataSource();

ocpds.setDriverType("oci");
ocpds.setServerName("dlsun999"); ocpds.setNetworkProtocol("tcp");
ocpds.setDatabaseName("816");
ocpds.setPortNumber(1521);
ocpds.setUser("scott");
ocpds.setPassword("tiger");

PooledConnection pc = ocpds.getPooledConnection();

Connection conn = pc.getConnection();


Connection Pooling – Reuse
Connection Pooling – Reuse
The end

Anda mungkin juga menyukai