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
<thomas@vandahl.org>. 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 @@
<name>J. Russell Smyth</name>
<email>drfish@cox.net</email>
</contributor>
+ <contributor>
+ <name>Thomas Vandahl</name>
+ <email>thomas@vandahl.org</email>
+ </contributor>
</contributors>
<licenses>
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 <a href="mailto:jmcnally@apache.org">John McNally</a>
+ * @author <a href="mailto:thomas@vandahl.org">Thomas Vandahl</a>
* @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 <code>DataSource</code> is to be available via
jndi. It is possible to use this factory to deploy a <code>DataSource</code>
into jndi, but in many cases for using this factory the <code>DataSource</code>
-is already deployed. This factory is specified with the following property:
+is already deployed. This factory is specified with the following property:
</p>
<source><![CDATA[
@@ -240,6 +240,24 @@
Such environment settings will likely not be necessary when running within
a J2EE container, but they are useful in other cases. One such case is when
running torque's unit/run-time tests
+</p>
+
+<p>
+One of the advantages of jndi is that it supports changing
+the <code>DataSource</code> on the fly. On the other hand this means that the
+actual <code>DataSource</code> 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 <code>DataSource</code> object for a
+configurable amount of time (im ms). The <b>t</b>ime between <b>t</b>wo
+<b>l</b>ookups is specified as follows:
+</p>
+
+<source><![CDATA[
+torque.dsfactory.bookstore.jndi.ttl=60000
+]]></source>
+
+<p>
+This property is optional. If not specified, it defaults to 0 (no caching).
</p>
</subsection>
---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org
|