db-torque-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Quinton McCombs" <qmcco...@nequalsone.com>
Subject RE: Getting access to the db connections
Date Tue, 25 Mar 2003 00:05:28 GMT
I am doing exactly the same thing although for different reasons.  The
idea is to be able to configure a listener that will be notified when a
new connection is obtained.  I am not very happy with the way that I
have implemented this but it does work.  

Here is a patch against the head version of Torque.java

########### cut here
Index: Torque.java
===================================================================
RCS file: /home/cvs/db-torque/src/java/org/apache/torque/Torque.java,v
retrieving revision 1.79
diff -u -r1.79 Torque.java
--- Torque.java	27 Feb 2003 17:37:42 -0000	1.79
+++ Torque.java	24 Mar 2003 23:57:05 -0000
@@ -69,6 +69,7 @@
 
 import org.apache.commons.configuration.Configuration;
 import org.apache.commons.configuration.PropertiesConfiguration;
+import org.apache.commons.lang.StringUtils;
 import org.apache.log4j.Logger;
 import org.apache.log4j.PropertyConfigurator;
 import org.apache.log4j.helpers.NullEnumeration;
@@ -102,6 +103,8 @@
                Configurable,
                Disposable
 {
+    private static ConnectionListener connectionListener;
+
     /**
      * Name of property that specifies the default map builder and map.
      */
@@ -245,6 +248,26 @@
 
         // setup manager mappings
         initManagerMappings(configuration);
+
+        initListener(configuration);
+    }
+
+    private static final void initListener(Configuration configuration)
+    {
+        String className =
configuration.getString("connectionlistener");
+        logger.debug("Loading listener class "+className);
+        if( StringUtils.isNotEmpty( className))
+        {
+            Object obj = new Object();
+            try
+            {
+                connectionListener = (ConnectionListener)
Class.forName(className).newInstance();
+            }
+            catch(Throwable e)
+            {
+                logger.error("Could not create instance of listener",
e);
+            }
+        }
     }
 
     /**
@@ -884,6 +907,10 @@
         {
             dsf = (DataSourceFactory) dsFactoryMap.get(name);
             con = dsf.getDataSource().getConnection();
+            if( connectionListener != null )
+            {
+                connectionListener.onBorrow(con);
+            }
         }
         catch (Exception e)
         {
@@ -923,6 +950,10 @@
         {
             dsf = (DataSourceFactory) dsFactoryMap.get(name);
             con = dsf.getDataSource().getConnection(username,
password);
+            if( connectionListener != null )
+            {
+                connectionListener.onBorrow(con);
+            }
         }
         catch (Exception e)
         {
########### cut here

Here is the interface that I defined.  It lives in the org.apache.torque
package.

########### cut here
package org.apache.torque;

import java.sql.Connection;

public interface ConnectionListener
{
    public void onBorrow(Connection con);
}
########### cut here

Using this is incredibly easy.  Simply define a class that implements
ConnectionListener.  When a new connection is obtained from the pool,
Torque will call the onBorrow(Connection) method.  Inside this method,
all you have to do is call the stored procedure.

Oh, you will nee to modify torque config file adding the following line
(modified with your class name):

torque.connectionlistener=com.yourcompany.YourConnectionListener


--------------------------------------------
Quinton McCombs
NequalsOne - HealthCare marketing tools
mailto:qmccombs@NequalsOne.com
http://www.NequalsOne.com 

> -----Original Message-----
> From: Steve Lukshides [mailto:lukshide@us.ibm.com] 
> Sent: Monday, March 24, 2003 5:25 PM
> To: torque-user@db.apache.org
> Subject: Getting access to the db connections
> 
> 
> 
> 
> 
> 
> Hi All,
> 
> Our Oracle DB Admin insists that we run a stored procedure 
> for each connection made to the database.  I'm not an Oracle 
> DBA so forgive me if this explanation is less than clear.  As 
> a security measure he expects every connection made to the 
> database to call a stored procedure to set the application's 
> role.  The idea is to prevent someone from logging on to the 
> DB through SQL Plus with the user name and password that 
> Torque uses and gain rights to use the database.  If someone 
> were to discover the username/password that Torque uses and 
> then logs on through SQL Plus they would not gain any rights 
> until the SP is executed, which they are not likely to know 
> and will be difficult to discover.
> 
> So my question is, how can I get at each connection that 
> Torque establishes, preferably at the time it is established, 
> and use it to execute the SP on?  Is this practical?  Is 
> there a better way to do this?
> 
> Thanks,
> Steve Lukshides
> 
> Sr. I/T Specialist, IBM Global Services
> 1475 Phoenixville Pike, West Chester, PA 19380
> Phone: 610-989-0340
> Voice Mail: 610-578-2385  IBM Tie Line: 873-2385
> Email: lukshide@us.ibm.com
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
> For additional commands, e-mail: torque-user-help@db.apache.org
> 
> 
> 


Mime
View raw message