henning 2004/10/20 07:13:58 Modified: . Tag: TORQUE_3_1_BRANCH project-base.xml src/java/org/apache/torque/dsfactory Tag: TORQUE_3_1_BRANCH JndiDataSourceFactory.java xdocs Tag: TORQUE_3_1_BRANCH configuration-howto.xml Log: Applied the JNDI caching patch contributed by Thomas Vandahl . Thanks a lot! Revision Changes Path No revision No revision 1.9.2.7 +4 -0 db-torque/project-base.xml Index: project-base.xml =================================================================== RCS file: /home/cvs/db-torque/project-base.xml,v retrieving revision 1.9.2.6 retrieving revision 1.9.2.7 diff -u -r1.9.2.6 -r1.9.2.7 --- project-base.xml 16 Oct 2004 12:58:36 -0000 1.9.2.6 +++ project-base.xml 20 Oct 2004 14:13:58 -0000 1.9.2.7 @@ -244,6 +244,10 @@ J. Russell Smyth drfish@cox.net + + Thomas Vandahl + thomas@vandahl.org + No revision No revision 1.6.2.3 +35 -11 db-torque/src/java/org/apache/torque/dsfactory/JndiDataSourceFactory.java Index: JndiDataSourceFactory.java =================================================================== RCS file: /home/cvs/db-torque/src/java/org/apache/torque/dsfactory/JndiDataSourceFactory.java,v retrieving revision 1.6.2.2 retrieving revision 1.6.2.3 diff -u -r1.6.2.2 -r1.6.2.3 --- JndiDataSourceFactory.java 20 May 2004 04:35:14 -0000 1.6.2.2 +++ JndiDataSourceFactory.java 20 Oct 2004 14:13:58 -0000 1.6.2.3 @@ -39,7 +39,11 @@ * 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 John McNally + * @author Thomas Vandahl * @version $Id$ */ public class JndiDataSourceFactory @@ -55,21 +59,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; } /** @@ -116,6 +135,11 @@ path = c.getString(key); log.debug("JNDI path: " + path); } + else if (key.equals("ttl")) + { + ttl = c.getLong(key, ttl); + log.debug("Time between context lookups: " + ttl); + } else { if (env == null) No revision No revision 1.1.2.3 +19 -1 db-torque/xdocs/configuration-howto.xml Index: configuration-howto.xml =================================================================== RCS file: /home/cvs/db-torque/xdocs/configuration-howto.xml,v retrieving revision 1.1.2.2 retrieving revision 1.1.2.3 diff -u -r1.1.2.2 -r1.1.2.3 --- configuration-howto.xml 19 Aug 2004 07:20:51 -0000 1.1.2.2 +++ configuration-howto.xml 20 Oct 2004 14:13:58 -0000 1.1.2.3 @@ -200,7 +200,7 @@ This factory is used if the DataSource is to be available via jndi. It is possible to use this factory to deploy a DataSource into jndi, but in many cases for using this factory the DataSource -is already deployed. This factory is specified with the following property: +is already deployed. This factory is specified with the following property:

+ +

+One of the advantages of jndi is that it supports changing +the DataSource on the fly. On the other hand this means that the +actual DataSource object must be looked up in the context with +every database operation. To avoid this, the factory provides a simple +caching mechanism, which stores the DataSource object for a +configurable amount of time (im ms). The time between two +lookups is specified as follows: +

+ + + +

+This property is optional. If not specified, it defaults to 0 (no caching).

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