sbailliez 01/12/21 14:40:27
Modified: src/main/org/apache/tools/ant/taskdefs SQLExec.java
Log:
Introduce naive caching of drivers. It is only based on the driver
name while doing pair driver/classpath would be better.
I tested it with mssql and it is OK with 1000 calls.
Alternatively it speeds up things significantly. (about 10 times faster for
me)
PR: 2971
Submitted by: [EMAIL PROTECTED]
Revision Changes Path
1.28 +48 -8
jakarta-ant/src/main/org/apache/tools/ant/taskdefs/SQLExec.java
Index: SQLExec.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/SQLExec.java,v
retrieving revision 1.27
retrieving revision 1.28
diff -u -r1.27 -r1.28
--- SQLExec.java 2001/12/12 09:17:36 1.27
+++ SQLExec.java 2001/12/21 22:40:27 1.28
@@ -80,6 +80,7 @@
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.Properties;
+import java.util.Hashtable;
import java.sql.Connection;
import java.sql.Statement;
@@ -108,10 +109,20 @@
return new String[] {NORMAL, ROW};
}
}
-
-
- private int goodSql = 0, totalSql = 0;
+ /**
+ * Used for caching loaders / driver. This is to avoid
+ * getting an OutOfMemoryError when calling this task
+ * multiple times in a row.
+ */
+ private static Hashtable loaderMap = new Hashtable(3);
+
+ public boolean caching = true;
+
+ private int goodSql = 0;
+
+ private int totalSql = 0;
+
private Path classpath;
private AntClassLoader loader;
@@ -214,6 +225,11 @@
*/
private String encoding = null;
+
+ public void setCaching(boolean value){
+ caching = value;
+ }
+
/**
* Set the classpath for loading the driver.
*/
@@ -428,14 +444,30 @@
throw new BuildException("Source file does not exist!",
location);
}
Driver driverInstance = null;
- // Load the driver using the
try {
Class dc;
if (classpath != null) {
- log( "Loading " + driver + " using AntClassLoader with
classpath " + classpath,
- Project.MSG_VERBOSE );
-
- loader = new AntClassLoader(project, classpath);
+ // check first that it is not already loaded otherwise
+ // consecutive runs seems to end into an OutOfMemoryError
+ // or it fails when there is a native library to load
+ // several times.
+ // this is far from being perfect but should work in most
cases.
+ synchronized (loaderMap){
+ if (caching){
+ loader = (AntClassLoader)loaderMap.get(driver);
+ }
+ if (loader == null){
+ log( "Loading " + driver + " using AntClassLoader
with classpath " + classpath,
+ Project.MSG_VERBOSE );
+ loader = new AntClassLoader(project, classpath);
+ if (caching){
+ loaderMap.put(driver, loader);
+ }
+ } else {
+ log("Loading " + driver + " using a cached
AntClassLoader.",
+ Project.MSG_VERBOSE);
+ }
+ }
dc = loader.loadClass(driver);
}
else {
@@ -734,6 +766,14 @@
reader.close();
}
}
+ }
+
+ protected Hashtable getLoaderMap(){
+ return loaderMap;
+ }
+
+ protected AntClassLoader getLoader(){
+ return loader;
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>