Previously I had posted an example for setting up a database connection pool using Apache commons. I have rough drafted an application module and a sample script to use it. We still need logging and a few other things in the module, but it is a start. We could also develop module and properties for other information in the pool.
Here is the script to use the module.
1:response.setStatus(200);
2:response.setContentType("text/html");
3:writer = response.getWriter();
4:writer.println("<html><head><title>Phobos</title></head><body>");
5:
6:var conn = null;
7:var stmt = null;
8:var rset = null;
9:if (globals.oConnRunning) {
10: writer.println("Pool Running.<br>");
11:}
12:
13:writer.println("Creating connection.<br>");
14:conn = java.sql.DriverManager.getConnection("jdbc:apache:commons:dbcp:oConn");
15:writer.println("Creating statement.<br>");
16:var stmt = conn.createStatement();
17:writer.println("Executing statement.<br>");
18:rset = stmt.executeQuery('Select * from customer');
19:writer.println("Results:<br>");
20:var numcols = rset.getMetaData().getColumnCount();
21:while (rset.next()) {
22: for(i=1;i<=numcols;i++) {
23: writer.println("<br>" + rset.getString(i));
24: }
25:}
26:
27:writer.println(module.dbu.statsConnectionPool()+"<br>");
28:writer.println("Closing Pool.<br>");
29:module.dbu.closeConnectionPool();
30:if (globals.oConnRunning==false) {
31: writer.println("Pool Stopped.<br>");
32:}
33:writer.println("</body></html>");
34:writer.flush();
Here is a the module.
1:library.common.define(module, "dbu", function() {
2: // used to start connection pool
3: this.startConnectionPool = function() {
4: try {
5: java.lang.Class.forName("org.apache.derby.jdbc.ClientDriver");
6: // First, we'll need a ObjectPool that serves as the actual pool of connections.
7: var connectionPool = new Packages.org.apache.commons.pool.impl.GenericObjectPool();
8: // Set up our connect string for our database
9: var connectURI = 'jdbc:derby://localhost:1527/sample;user=app;password=app';
10: // Next, we'll create a ConnectionFactory that the pool will use to create Connections.
11: // We'll use the DriverManagerConnectionFactory
12: connectionFactory = new Packages.org.apache.commons.dbcp.DriverManagerConnectionFactory(connectURI,null);
13: // Now we'll create the PoolableConnectionFactory, which wraps
14: // the "real" Connections created by the ConnectionFactory with
15: // the classes that implement the pooling functionality.
16: poolableConnectionFactory = new Packages.org.apache.commons.dbcp.PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);
17: // Finally, we create the PoolingDriver itself
18: java.lang.Class.forName("org.apache.commons.dbcp.PoolingDriver");
19: // ...and register our pool with it.
20: var driver = java.sql.DriverManager.getDriver("jdbc:apache:commons:dbcp:");
21: // Now we can just use the connect string "jdbc:apache:commons:dbcp:example"
22: // to access our pool of Connections.
23: driver.registerPool("oConn",connectionPool);
24: globals.oConnRunning = true;
25: return true;
26: }
27: catch(exception) {
28: globals.oConnRunning = false;
29: return false;
30: }
31: }// startConnectionPool
32:
33: // Close the connection pool
34: this.closeConnectionPool = function() {
35: try {
36: var driver = java.sql.DriverManager.getDriver("jdbc:apache:commons:dbcp:");
37: driver.closePool("oConn");
38: globals.oConnRunning = false;
39: return true;
40: }
41: catch(exception) {
42: return false;
43: }
44: }// Close connection pool
45:
46: // Connection pool stats
47: //Returns number active and idle in format active,idle eg 3,10
48: this.statsConnectionPool = function() {
49: var driver = java.sql.DriverManager.getDriver("jdbc:apache:commons:dbcp:");
50: connectionPool = driver.getConnectionPool("oConn");
51: var stats = connectionPool.getNumActive() + "," + connectionPool.getNumIdle();
52: return stats;
53: }//stats connection Pool
54:});
55:
0 comments:
Post a Comment