提交 f5339ed9 authored 作者: Thomas Mueller's avatar Thomas Mueller

--no commit message

--no commit message
上级 4715ebcd
...@@ -10,7 +10,8 @@ import java.lang.ref.WeakReference; ...@@ -10,7 +10,8 @@ import java.lang.ref.WeakReference;
/** /**
* This class is responsible to close a database if the application did not * This class is responsible to close a database if the application did not
* close a connection. * close a connection. A database closer object only exists if there is no user
* connected to the database.
*/ */
public class DatabaseCloser extends Thread { public class DatabaseCloser extends Thread {
...@@ -25,6 +26,10 @@ public class DatabaseCloser extends Thread { ...@@ -25,6 +26,10 @@ public class DatabaseCloser extends Thread {
this.shutdownHook = shutdownHook; this.shutdownHook = shutdownHook;
} }
/**
* Stop and disable the database closer. This method is called after the
* database has been closed, or after a session has been created.
*/
public void reset() { public void reset() {
synchronized (this) { synchronized (this) {
databaseRef = null; databaseRef = null;
......
...@@ -94,11 +94,17 @@ public class Engine { ...@@ -94,11 +94,17 @@ public class Engine {
throw Message.getSQLException(ErrorCode.WRONG_USER_OR_PASSWORD); throw Message.getSQLException(ErrorCode.WRONG_USER_OR_PASSWORD);
} }
checkClustering(ci, database); checkClustering(ci, database);
Session session = database.createUserSession(user); Session session = database.createSession(user);
return session; return session;
} }
} }
/**
* Open a database connection with the given connection information.
*
* @param ci the connection information
* @return the session
*/
public Session getSession(ConnectionInfo ci) throws SQLException { public Session getSession(ConnectionInfo ci) throws SQLException {
try { try {
Session session = openSession(ci); Session session = openSession(ci);
...@@ -171,6 +177,12 @@ public class Engine { ...@@ -171,6 +177,12 @@ public class Engine {
} }
} }
/**
* Called after a database has been closed, to remove the object from the
* list of open databases.
*
* @param name the database name
*/
public void close(String name) { public void close(String name) {
databases.remove(name); databases.remove(name);
} }
......
...@@ -17,6 +17,7 @@ import org.h2.expression.Expression; ...@@ -17,6 +17,7 @@ import org.h2.expression.Expression;
import org.h2.message.Message; import org.h2.message.Message;
import org.h2.message.Trace; import org.h2.message.Trace;
import org.h2.table.Table; import org.h2.table.Table;
import org.h2.util.ClassUtils;
import org.h2.value.DataType; import org.h2.value.DataType;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueNull; import org.h2.value.ValueNull;
...@@ -57,7 +58,7 @@ public class FunctionAlias extends DbObjectBase { ...@@ -57,7 +58,7 @@ public class FunctionAlias extends DbObjectBase {
if (javaMethod != null) { if (javaMethod != null) {
return; return;
} }
Class javaClass = database.loadUserClass(className); Class javaClass = ClassUtils.loadUserClass(className);
Method[] methods = javaClass.getMethods(); Method[] methods = javaClass.getMethods();
for (int i = 0; i < methods.length; i++) { for (int i = 0; i < methods.length; i++) {
Method m = methods[i]; Method m = methods[i];
...@@ -150,10 +151,6 @@ public class FunctionAlias extends DbObjectBase { ...@@ -150,10 +151,6 @@ public class FunctionAlias extends DbObjectBase {
throw Message.getUnsupportedException(); throw Message.getUnsupportedException();
} }
public Value getValue(Session session, Expression[] args) throws SQLException {
return getValue(session, args, false);
}
/** /**
* Call the user-defined function and return the value. * Call the user-defined function and return the value.
* *
...@@ -228,6 +225,11 @@ public class FunctionAlias extends DbObjectBase { ...@@ -228,6 +225,11 @@ public class FunctionAlias extends DbObjectBase {
return this.methodName; return this.methodName;
} }
/**
* Check if this function requires a database connection.
*
* @return if the function requires a connection
*/
public boolean hasConnectionParam() { public boolean hasConnectionParam() {
return this.hasConnectionParam; return this.hasConnectionParam;
} }
......
...@@ -13,6 +13,7 @@ import org.h2.command.Parser; ...@@ -13,6 +13,7 @@ import org.h2.command.Parser;
import org.h2.message.Message; import org.h2.message.Message;
import org.h2.message.Trace; import org.h2.message.Trace;
import org.h2.table.Table; import org.h2.table.Table;
import org.h2.util.ClassUtils;
/** /**
* Represents a user-defined aggregate function. * Represents a user-defined aggregate function.
...@@ -32,7 +33,7 @@ public class UserAggregate extends DbObjectBase { ...@@ -32,7 +33,7 @@ public class UserAggregate extends DbObjectBase {
public AggregateFunction getInstance() throws SQLException { public AggregateFunction getInstance() throws SQLException {
if (javaClass == null) { if (javaClass == null) {
javaClass = database.loadUserClass(className); javaClass = ClassUtils.loadUserClass(className);
} }
Object obj; Object obj;
try { try {
......
...@@ -31,7 +31,7 @@ public class JavaFunction extends Expression implements FunctionCall { ...@@ -31,7 +31,7 @@ public class JavaFunction extends Expression implements FunctionCall {
} }
public Value getValue(Session session) throws SQLException { public Value getValue(Session session) throws SQLException {
return functionAlias.getValue(session, args); return functionAlias.getValue(session, args, false);
} }
public int getType() { public int getType() {
......
...@@ -18,6 +18,7 @@ import org.h2.message.Message; ...@@ -18,6 +18,7 @@ import org.h2.message.Message;
import org.h2.message.Trace; import org.h2.message.Trace;
import org.h2.result.Row; import org.h2.result.Row;
import org.h2.table.Table; import org.h2.table.Table;
import org.h2.util.ClassUtils;
import org.h2.value.DataType; import org.h2.value.DataType;
import org.h2.value.Value; import org.h2.value.Value;
...@@ -55,7 +56,7 @@ public class TriggerObject extends SchemaObjectBase { ...@@ -55,7 +56,7 @@ public class TriggerObject extends SchemaObjectBase {
} }
try { try {
Connection c2 = session.createConnection(false); Connection c2 = session.createConnection(false);
Object obj = session.getDatabase().loadUserClass(triggerClassName).newInstance(); Object obj = ClassUtils.loadUserClass(triggerClassName).newInstance();
triggerCallback = (Trigger) obj; triggerCallback = (Trigger) obj;
triggerCallback.init(c2, getSchema().getName(), getName(), table.getName(), before, typeMask); triggerCallback.init(c2, getSchema().getName(), getName(), table.getName(), before, typeMask);
} catch (Throwable e) { } catch (Throwable e) {
......
...@@ -103,11 +103,7 @@ public class RunScript extends Tool { ...@@ -103,11 +103,7 @@ public class RunScript extends Tool {
showTime = true; showTime = true;
} else if (arg.equals("-driver")) { } else if (arg.equals("-driver")) {
String driver = args[++i]; String driver = args[++i];
try { ClassUtils.loadUserClass(driver);
ClassUtils.loadUserClass(driver);
} catch (ClassNotFoundException e) {
throw Message.convert(e);
}
} else if (arg.equals("-options")) { } else if (arg.equals("-options")) {
StringBuffer buff = new StringBuffer(); StringBuffer buff = new StringBuffer();
i++; i++;
......
...@@ -20,7 +20,6 @@ import java.sql.Statement; ...@@ -20,7 +20,6 @@ import java.sql.Statement;
import java.util.Properties; import java.util.Properties;
import org.h2.engine.Constants; import org.h2.engine.Constants;
import org.h2.message.Message;
import org.h2.server.web.ConnectionInfo; import org.h2.server.web.ConnectionInfo;
import org.h2.util.ClassUtils; import org.h2.util.ClassUtils;
import org.h2.util.FileUtils; import org.h2.util.FileUtils;
...@@ -82,11 +81,7 @@ public class Shell { ...@@ -82,11 +81,7 @@ public class Shell {
password = args[++i]; password = args[++i];
} else if (args[i].equals("-driver")) { } else if (args[i].equals("-driver")) {
String driver = args[++i]; String driver = args[++i];
try { ClassUtils.loadUserClass(driver);
ClassUtils.loadUserClass(driver);
} catch (ClassNotFoundException e) {
throw Message.convert(e);
}
} else { } else {
out.println("Unsupported option: " + args[i]); out.println("Unsupported option: " + args[i]);
showUsage(); showUsage();
......
...@@ -48,7 +48,7 @@ public class ClassUtils { ...@@ -48,7 +48,7 @@ public class ClassUtils {
return Class.forName(className); return Class.forName(className);
} }
public static Class loadUserClass(String className) throws ClassNotFoundException, SQLException { public static Class loadUserClass(String className) throws SQLException {
if (!ALLOW_ALL && !ALLOWED_CLASS_NAMES.contains(className)) { if (!ALLOW_ALL && !ALLOWED_CLASS_NAMES.contains(className)) {
boolean allowed = false; boolean allowed = false;
for (int i = 0; i < ALLOWED_CLASS_NAME_PREFIXES.length; i++) { for (int i = 0; i < ALLOWED_CLASS_NAME_PREFIXES.length; i++) {
...@@ -61,7 +61,13 @@ public class ClassUtils { ...@@ -61,7 +61,13 @@ public class ClassUtils {
throw Message.getSQLException(ErrorCode.ACCESS_DENIED_TO_CLASS_1, className); throw Message.getSQLException(ErrorCode.ACCESS_DENIED_TO_CLASS_1, className);
} }
} }
return Class.forName(className); try {
return Class.forName(className);
} catch (ClassNotFoundException e) {
throw Message.getSQLException(ErrorCode.CLASS_NOT_FOUND_1, new String[] { className }, e);
} catch (NoClassDefFoundError e) {
throw Message.getSQLException(ErrorCode.CLASS_NOT_FOUND_1, new String[] { className }, e);
}
} }
} }
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
*/ */
package org.h2.util; package org.h2.util;
import java.sql.SQLException;
/** /**
* This class tries to automatically load the right JDBC driver for a given * This class tries to automatically load the right JDBC driver for a given
* database URL. * database URL.
...@@ -50,10 +52,10 @@ public class JdbcDriverUtils { ...@@ -50,10 +52,10 @@ public class JdbcDriverUtils {
return null; return null;
} }
public static void load(String url) throws ClassNotFoundException { public static void load(String url) throws SQLException {
String driver = getDriver(url); String driver = getDriver(url);
if (driver != null) { if (driver != null) {
Class.forName(driver); ClassUtils.loadUserClass(driver);
} }
} }
......
...@@ -13,14 +13,11 @@ import java.sql.SQLException; ...@@ -13,14 +13,11 @@ import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.Properties; import java.util.Properties;
//## Java 1.4 begin ##
import javax.naming.Context; import javax.naming.Context;
import javax.naming.NamingException; import javax.naming.NamingException;
import javax.sql.DataSource; import javax.sql.DataSource;
import javax.sql.XAConnection; import javax.sql.XAConnection;
//## Java 1.4 end ##
import org.h2.constant.ErrorCode;
import org.h2.message.Message; import org.h2.message.Message;
/** /**
...@@ -95,43 +92,37 @@ public class JdbcUtils { ...@@ -95,43 +92,37 @@ public class JdbcUtils {
} }
public static Connection getConnection(String driver, String url, Properties prop) throws SQLException { public static Connection getConnection(String driver, String url, Properties prop) throws SQLException {
try { if (StringUtils.isNullOrEmpty(driver)) {
if (StringUtils.isNullOrEmpty(driver)) { JdbcDriverUtils.load(url);
JdbcDriverUtils.load(url); } else {
} else { Class d = ClassUtils.loadUserClass(driver);
Class d = ClassUtils.loadUserClass(driver); if (java.sql.Driver.class.isAssignableFrom(d)) {
if (java.sql.Driver.class.isAssignableFrom(d)) { return DriverManager.getConnection(url, prop);
return DriverManager.getConnection(url, prop); //## Java 1.4 begin ##
//## Java 1.4 begin ## } else if (javax.naming.Context.class.isAssignableFrom(d)) {
} else if (javax.naming.Context.class.isAssignableFrom(d)) { // JNDI context
// JNDI context try {
try { Context context = (Context) d.newInstance();
Context context = (Context) d.newInstance(); DataSource ds = (DataSource) context.lookup(url);
DataSource ds = (DataSource) context.lookup(url); String user = prop.getProperty("user");
String user = prop.getProperty("user"); String password = prop.getProperty("password");
String password = prop.getProperty("password"); if (StringUtils.isNullOrEmpty(user) && StringUtils.isNullOrEmpty(password)) {
if (StringUtils.isNullOrEmpty(user) && StringUtils.isNullOrEmpty(password)) { return ds.getConnection();
return ds.getConnection(); } else {
} else { return ds.getConnection(user, password);
return ds.getConnection(user, password); }
} } catch (InstantiationException e) {
} catch (InstantiationException e) { throw Message.convert(e);
throw Message.convert(e); } catch (IllegalAccessException e) {
} catch (IllegalAccessException e) { throw Message.convert(e);
throw Message.convert(e); } catch (NamingException e) {
} catch (NamingException e) { throw Message.convert(e);
throw Message.convert(e);
}
//## Java 1.4 end ##
} else {
// Don't know, but maybe it loaded a JDBC Driver
return DriverManager.getConnection(url, prop);
} }
} //## Java 1.4 end ##
} catch (ClassNotFoundException e) { } else {
throw Message.getSQLException(ErrorCode.CLASS_NOT_FOUND_1, new String[]{driver}, e); // Don't know, but maybe it loaded a JDBC Driver
} catch (NoClassDefFoundError e) { return DriverManager.getConnection(url, prop);
throw Message.getSQLException(ErrorCode.CLASS_NOT_FOUND_1, new String[]{driver}, e); }
} }
return DriverManager.getConnection(url, prop); return DriverManager.getConnection(url, prop);
} }
......
...@@ -171,6 +171,7 @@ public class Build extends BuildBase { ...@@ -171,6 +171,7 @@ public class Build extends BuildBase {
javadoc(new String[] { javadoc(new String[] {
"-sourcepath", "src/main" + File.pathSeparator + "src/test" + File.pathSeparator + "src/tools" , "-sourcepath", "src/main" + File.pathSeparator + "src/test" + File.pathSeparator + "src/tools" ,
"-noindex", "-noindex",
"-d", "docs/javadocImpl2",
"-classpath", "ext/servlet-api-2.4.jar" + File.pathSeparator + "ext/lucene-core-2.2.0.jar" + "-classpath", "ext/servlet-api-2.4.jar" + File.pathSeparator + "ext/lucene-core-2.2.0.jar" +
File.pathSeparator + System.getProperty("java.home") + "/../lib/tools.jar", File.pathSeparator + System.getProperty("java.home") + "/../lib/tools.jar",
"-subpackages", "org.h2", "-subpackages", "org.h2",
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论