Browse Source

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
master
Stephane Bailliez 23 years ago
parent
commit
672481d0ae
1 changed files with 48 additions and 8 deletions
  1. +48
    -8
      src/main/org/apache/tools/ant/taskdefs/SQLExec.java

+ 48
- 8
src/main/org/apache/tools/ant/taskdefs/SQLExec.java View File

@@ -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;
}

}

Loading…
Cancel
Save