I l @ ve RuBoard |
Using Turbine Connection PoolingUsing 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 |