From 672481d0aed938db880c9691736877e74cde875c Mon Sep 17 00:00:00 2001 From: Stephane Bailliez Date: Fri, 21 Dec 2001 22:40:27 +0000 Subject: [PATCH] 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: stephen.wong@everypath.com git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@270294 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/tools/ant/taskdefs/SQLExec.java | 56 ++++++++++++++++--- 1 file changed, 48 insertions(+), 8 deletions(-) diff --git a/src/main/org/apache/tools/ant/taskdefs/SQLExec.java b/src/main/org/apache/tools/ant/taskdefs/SQLExec.java index 34c2c202e..54c367ce7 100644 --- a/src/main/org/apache/tools/ant/taskdefs/SQLExec.java +++ b/src/main/org/apache/tools/ant/taskdefs/SQLExec.java @@ -80,6 +80,7 @@ import java.util.Enumeration; 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,9 +109,19 @@ public class SQLExec extends Task { 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; @@ -214,6 +225,11 @@ public class SQLExec extends Task { */ private String encoding = null; + + public void setCaching(boolean value){ + caching = value; + } + /** * Set the classpath for loading the driver. */ @@ -428,14 +444,30 @@ public class SQLExec extends Task { 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 { @@ -736,4 +768,12 @@ public class SQLExec extends Task { } } + protected Hashtable getLoaderMap(){ + return loaderMap; + } + + protected AntClassLoader getLoader(){ + return loader; + } + }