Connection Pooling

Establishing a connection to a database can be very expensive and time consuming. An application can easily spend several seconds every time it needs to establish a connection to perform a database transaction that might take milliseconds. JDBC Connection Pools can alleviate this overhead by sharing a "pool" of open connections to the database. Applications can simply peal off an open connection from the pool and return it back when it is done. This approach significantly improves the speed and scalability of your database-driven applications.

The javaxt.sql.Connection class can use a standard JDBC Connections stored in a JDBC connection pool. Here are a few examples.

Web Applications

Most modern web application servers support JDBC connection pools. Java Servlets and JSP pages can leverage these connection pools using the javax.naming.InitialContext class. In this example, we will open a javaxt.sql.Connection to the "NorthWind" database using the "jdbc/Northwind" JDBC connection pool.

//Find a database connection
  javax.naming.InitialContext ctx = new javax.naming.InitialContext();
  javax.sql.DataSource ds = (javax.sql.DataSource)ctx.lookup("jdbc/Northwind");

//Open a connection to the database
  javaxt.sql.Connection conn = new javaxt.sql.Connection();
  conn.open(ds.getConnection());

//Do something with the connection. In this case, we will open a Recordset and execute a query
  javaxt.sql.Recordset rs = new javaxt.sql.Recordset();
  rs.open("SELECT * FROM EMPLOYEE", conn);  
  rs.close(); 
 
//Close connection so it can be released back to the pool!
  conn.close();

Stand Alone Applications

The javaxt.sql.ConnectionPool class can be used to create a connection pool for a stand alone GUI or command line applications. In this example, we will reuse a standard JDBC Connection stored in a JDBC connection pool.

//Remove a jdbc connection from the connection pool and pass it to a javaxt connection
  javaxt.sql.Connection conn = connectionPool.getConnection();

//Do something with the connection. In this case, we will open a Recordset and execute a query
  javaxt.sql.Recordset rs = new javaxt.sql.Recordset();
  rs.open("SELECT * FROM EMPLOYEE", conn);  
  rs.close(); 
 
//Close connection so it can be released back to the pool!
  conn.close();