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

--no commit message

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