John McNally wrote:

The reason it is not cached is that the DataSource/JNDI concept allows an adminstration application to change the DataSource that is bound to a name and an application using that name would automatically start using a different pool of connections.

Ok. I propose the following patch. It creates a very simple "cache" to keep the DataSource object for a configurable time (default 0) and then performs a new lookup. Does this look reasonable?


Bye, Thomas.

diff -Naur src/java/org/apache/torque/dsfactory/JndiDataSourceFactory.java 
new-src/java/org/apache/torque/dsfactory/JndiDataSourceFactory.java
--- src/java/org/apache/torque/dsfactory/JndiDataSourceFactory.java     Thu Sep  4 
07:52:26 2003
+++ new-src/java/org/apache/torque/dsfactory/JndiDataSourceFactory.java Thu Oct  7 
14:49:56 2004
@@ -77,6 +77,9 @@
  * to deploy the DataSource based on properties found in the
  * configuration.
  *
+ * This factory tries to avoid excessive context lookups to improve speed.
+ * The time between two lookups can be configured. The default is 0 (no cache).
+ *
  * @author <a href="mailto:[EMAIL PROTECTED]">John McNally</a>
  * @version $Id: JndiDataSourceFactory.java,v 1.6 2003/05/14 19:38:04 mpoeschl Exp $
  */
@@ -93,21 +96,36 @@
     /** The context to get the resource from. */
     private Context ctx;
 
+    /** A locally cached copy of the DataSource */
+    private DataSource ds = null;
+
+    /** Time of last actual lookup action */
+    private long lastLookup = 0;
+
+    /** Time between two lookups */
+    private long ttl = 0; // ms
+
     /**
      * @see org.apache.torque.dsfactory.DataSourceFactory#getDataSource
      */
     public DataSource getDataSource() throws TorqueException
     {
-        DataSource ds = null;
-        try
-        {
-            ds = ((DataSource) ctx.lookup(path));
-        }
-        catch (Exception e)
-        {
-            throw new TorqueException(e);
-        }
-        return ds;
+       long time = System.currentTimeMillis();
+       
+       if (ds == null || time - lastLookup > ttl)
+       {
+            try
+            {
+                ds = ((DataSource) ctx.lookup(path));
+               lastLookup = time;
+            }
+            catch (Exception e)
+            {
+                throw new TorqueException(e);
+            }
+       }
+
+       return ds;
     }
 
     /**
@@ -154,7 +172,12 @@
                     path = c.getString(key);
                     log.debug("JNDI path: " + path);
                 }
-                else
+                else if (key.equals("ttl"))
+               {
+                   ttl = c.getLong(key, ttl);
+                    log.debug("Time between context lookups: " + ttl);
+               }
+               else
                 {
                     if (env == null)
                     {

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to