Using Turbine Connection Pooling

I l @ ve RuBoard

Using Turbine Connection Pooling

Using the connection-pooling code is a pretty simple affair, as shown by the sample program in Listing 7.6.

Listing 7.6 DBPool.jsp
 <%@ page import="org.apache.turbine.util.TurbineConfig" %> <%@ page import="org.apache.turbine.services.db.TurbineDB" %> <%@ page import="org.apache.turbine.util.db.pool.DBConnection" %> <%@ page import="java.sql.*" %> <%@ page import="org.apache.turbine.util.Log" %> <HTML> <HEAD> <TITLE>A JDBC SmokeTest</TITLE> </HEAD> <BODY> This is a smoketest <% DBConnection dbConn = TurbineDB.getConnection(); PreparedStatement pstmt = dbConn.prepareStatement("select * from Customer"); ResultSet rs = pstmt.executeQuery(); if (rs.next()) {     out.print("Got a result set!"); }  else {     out.print("No result set found!!"); } rs.close(); pstmt.close(); TurbineDB.releaseConnection(dbConn); %> </BODY> </HTML> 

When Turbine.DB is sent a getConnectio n message, it allocates a DBConnection object from the pool, if one is available. If no object is available and the pool size has not been exceeded, Turbine.DB creates a new one; otherwise , it waits for a connection to free up. It uses the database connection information specified in the TurbineResources.properties file, which is in your application's base directory in Tomcat.

The DBConnection object wraps the java.sql.Connection object, which allows you to use createStatement and prepareStatement with it. When you are finished with the connection, you use releaseConnection to return it to the pool. In production code, you should always wrap all the code after the getConnection in a try and have the catch do a releaseConnection , to avoid leaks.

I l @ ve RuBoard


MySQL and JSP Web Applications. Data-Driven Programming Using Tomcat and MySQL
MySQL and JSP Web Applications: Data-Driven Programming Using Tomcat and MySQL
ISBN: 0672323095
EAN: 2147483647
Year: 2002
Pages: 203
Authors: James Turner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net