27.10.2013 Views

Jaybird 2.1 JDBC driver Java Programmer's Manual - Firebird

Jaybird 2.1 JDBC driver Java Programmer's Manual - Firebird

Jaybird 2.1 JDBC driver Java Programmer's Manual - Firebird

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1<br />

2<br />

3<br />

4<br />

5<br />

6<br />

package hello;<br />

import java.sql.*;<br />

import javax.sql.*;<br />

import org.firebirdsql.pool.*;<br />

public class HelloConnectionPool {<br />

}<br />

public static void main(String[] args) throws Exception {<br />

}<br />

org.firebirdsql.pool.FBConnectionPoolDataSource pool =<br />

new org.firebirdsql.pool.FBConnectionPoolDataSource();<br />

pool.setMaxPoolSize(5);<br />

pool.setMinPoolSize(2);<br />

pool.setMaxStatements(10);<br />

pool.setMaxIdleTime(30 * 60 * 60);<br />

pool.setDatabase("localhost/3050:C:/db/employee.fdb");<br />

pool.setUser("SYSDBA");<br />

pool.setPassword("masterkey");<br />

// obtain a physical connection to the database<br />

PooledConnection pooledCon = pool.getPooledConnection();<br />

// obtain a wrapped connection<br />

Connection connection = pooledCon.getConnection();<br />

try {<br />

// do something here...<br />

} finally {<br />

}<br />

// release the connection back to pool<br />

connection.close();<br />

Illustration 2.8.: Example of instantiating and using the javax.sql.ConnectionPoolDataSource<br />

implementation<br />

The code on Illustration 2.8 we perform the following steps:<br />

1. Create a connection pool object. In this example we create instance<br />

implementing the javax.sql.ConnectionPoolDataSource interface.<br />

2. Now we specify the pooling properties: maximum of 5 physical connections,<br />

with minimum of 2, and each connection will maintain a cache of 10 prepared<br />

statements of the same type (i.e. with the same SQL). Connections in the pool<br />

that are idle for more than half an hour (30 * 60 * 60 seconds) are closed<br />

automatically.<br />

3. After specifying the pooling properties we set the database connection<br />

properties. In our case that is only database path, user name and password, but<br />

also any other supported property can be set here.<br />

4. Having configured the data source, we obtain the physical connection to the<br />

database. Our data source implementation will check the internal connection<br />

pool and will open a new physical connection to the database if the pool is<br />

empty. An instance of javax.sql.PooledConnection represents a physical<br />

connection to the database. Calling the PooledConnection.close() method<br />

Chapter 2. Obtaining a connection 16

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!