[prev in list] [next in list] [prev in thread] [next in thread] 

List:       jakarta-commons-dev
Subject:    Re: [DBCP] proposal for activationStatement and passivationStatement in PoolableConnectionFactory
From:       Brent Verner <brent () rcfile ! org>
Date:       2003-12-22 11:49:18
[Download RAW message or body]

[2003-12-21 21:36] Dirk Verbeeck said:
| Yes this would be a nice feature, a couple of days ago there was 
| somebody else requesting a similar thing on commons-user.
| A patch is very welcome.

patch attached.  While cleaning this up, I realized that 
activation/passivation was executed each time a pooled
connection was borrowed.  This is not at all what I'd 
originally intended...<blush />   As such, I've moved the 
statement execution calls into makeObject() and destroyObject()
where the do what I want (only once :-)).

| One remark, I would stop the normal processing when an exception is 
| thrown. If needed you can always handle error in a PL/SQL block or 
| stored procedure.

Ok. An SQLException is thrown on error.

cheers.
  Brent


P.S. I'll be slow to reply to email over the next couple of
     weeks...  happy holidays!

-- 
"Develop your talent, man, and leave the world something. Records are 
really gifts from people. To think that an artist would love you enough
to share his music with anyone is a beautiful thing."  -- Duane Allman

["pcf.diff" (text/plain)]

Index: src/java/org/apache/commons/dbcp/PoolableConnectionFactory.java
===================================================================
RCS file: /home/cvspublic/jakarta-commons/dbcp/src/java/org/apache/commons/dbcp/PoolableConnectionFactory.java,v
 retrieving revision 1.17
diff -u -r1.17 PoolableConnectionFactory.java
--- src/java/org/apache/commons/dbcp/PoolableConnectionFactory.java	30 Nov 2003 \
                19:01:33 -0000	1.17
+++ src/java/org/apache/commons/dbcp/PoolableConnectionFactory.java	22 Dec 2003 \
11:33:19 -0000 @@ -295,9 +295,35 @@
     public void setDefaultCatalog(String defaultCatalog) {
         _defaultCatalog = defaultCatalog;
     }
+
+    /**
+     * Defines a statement to be executed immediately after Connection is created
+     * in {@link #makeObject}.
+     * 
+     * Useful for performing additional Conection configuration such as
+     * "ALTER SESSION"-type statements.
+     *
+     * @param initializationStatement statement to be executed during Connection \
activation +     */
+    synchronized public void setInitializationStatement(String \
initializationStatement){ +        _initializationStatement = \
initializationStatement; +    }
+
+    /**
+     * Defines a statement to be executed immediately before Connection is closed
+     * in {@link #destroyObject}.
+     * 
+     * @param destructionStatement statement to be executed during Connection \
activation +     */
+    synchronized public void setDestructionStatement(String destructionStatement){
+        _destructionStatement = destructionStatement;
+    }
     
     synchronized public Object makeObject() throws Exception {
         Connection conn = _connFactory.createConnection();
+        if( _initializationStatement != null ){
+            executeStatement(conn, "initializationStatement", \
_initializationStatement); +        }
         if(null != _stmtPoolFactory) {
             KeyedObjectPool stmtpool = _stmtPoolFactory.createPool();
             conn = new PoolingConnection(conn,stmtpool);
@@ -308,10 +334,28 @@
 
     public void destroyObject(Object obj) throws Exception {
         if(obj instanceof PoolableConnection) {
+            if( _destructionStatement != null ){
+                executeStatement((Connection)obj, "destructionStatement", \
_destructionStatement); +            }
             ((PoolableConnection)obj).reallyClose();
         }
     }
 
+    protected void executeStatement(Connection conn, String type, String sql)
+    throws SQLException {
+       Statement st = conn.createStatement();
+       try {
+           st.execute(sql);
+       }
+       catch( SQLException ex ){
+           throw new SQLException(type + " [" + sql + "] failed: " + \
ex.getMessage()); +       }
+       finally {
+           try { st.close(); }
+           catch( Exception ex ){ }
+       }
+    }
+
     public boolean validateObject(Object obj) {
         if(obj instanceof Connection) {
             try {
@@ -394,6 +438,8 @@
     protected boolean _defaultAutoCommit = true;
     protected int _defaultTransactionIsolation = UNKNOWN_TRANSACTIONISOLATION;
     protected String _defaultCatalog;
+    protected String _initializationStatement;
+    protected String _destructionStatement;
     
     /**
      * @deprecated AbandonedConfig is now deprecated.



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic