background image
<< Accessing the Network Server | XA and the Network Server >>

Statement caching example

<< Accessing the Network Server | XA and the Network Server >>
Derby Server and Administration Guide
28
connection, the cached object is fetched from the cache, and the
prepareStatement
call seems to have succeeded, although the statement has not actually been prepared.
When the prepared statement is executed, the error is detected on the server side, and
the client is notified.
DataSource access examples
The following example uses
org.apache.derby.jdbc.ClientDataSource
to
access the Network Server:
org.apache.derby.jdbc.ClientDataSource ds =
new org.apache.derby.jdbc.ClientDataSource();
ds.setDatabaseName("mydb");
ds.setCreateDatabase("create");
ds.setUser("user");
ds.setPassword("mypass");
// The host on which Network Server is running
ds.setServerName("localhost");
// The port on which Network Server is listening
ds.setPortNumber(1527);
Connection conn = ds.getConnection();
Statement caching example
The following example uses
org.apache.derby.jdbc.ClientConnectionPoolDataSource
to access the
Network Server and use JDBC statement caching:
org.apache.derby.jdbc.ClientConnectionPoolDataSource cpds =
new ClientConnectionPoolDataSource();
// Set the number of statements the cache is allowed to cache.
// Any number greater than zero will enable the cache.
cpds.setMaxStatements(20);
// Set other DataSource properties
cpds.setDatabaseName("mydb");
cpds.setCreateDatabase("create");
cpds.setUser("user");
cpds.setPassword("mypass");
cpds.setServerName("localhost");
cpds.setPortNumber(1527);
// This physical connection will have JDBC statement caching enabled.
javax.sql.PooledConnection pc = cpds.getPooledConnection();
// Create a logical connection.
java.sql.Connection con = pc.getConnection();
// Interact with the database.
java.sql.PreparedStatement ps = con.prepareStatement(
"select * from myTable where id = ?");
...
ps.close(); // Inserts or returns statement to the cache
...
con.close();
// The next logical connection can gain from using the cache.
con = pc.getConnection();
// This prepare causes a statement to be fetched from the local cache.
PreparedStatement ps = con.prepareStatement(
"select * from myTable where id = ?");
...