提交 3ee41492 authored 作者: Thomas Mueller's avatar Thomas Mueller

New database setting DEFAULT_CONNECTION (disabled by default) to support…

New database setting DEFAULT_CONNECTION (disabled by default) to support DriverManager.getConnection("jdbc:default:connection"). Please note the Oracle JDBC driver will try to resolve this database URL if it is loaded before the H2 driver.
上级 799ce225
......@@ -18,7 +18,10 @@ Change Log
<h1>Change Log</h1>
<h2>Next Version (unreleased)</h2>
<ul><li>JaQu: the static map Db.TOKENS was not synchronized.
<ul><li>New database setting DEFAULT_CONNECTION (disabled by default)
to support DriverManager.getConnection("jdbc:default:connection").
Please note the Oracle JDBC driver will try to resolve this database URL if it is loaded before the H2 driver.
</li><li>JaQu: the static map Db.TOKENS was not synchronized.
</li><li>DatabaseMetaData.getProcedureColumns returned the wrong data
(it also returned the Connection parameter if there was any).
</li><li>Issue 284: If the query cache was used (enabled by default in version 1.3.x),
......
......@@ -32,6 +32,9 @@ import org.h2.upgrade.DbUpgrade;
public class Driver implements java.sql.Driver {
private static final Driver INSTANCE = new Driver();
private static final String DEFAULT_URL = "jdbc:default:connection";
private static final ThreadLocal<Connection> DEFAULT_CONNECTION = new ThreadLocal<Connection>();
private static volatile boolean registered;
static {
......@@ -55,6 +58,9 @@ public class Driver implements java.sql.Driver {
if (!acceptsURL(url)) {
return null;
}
if (url.equals(DEFAULT_URL)) {
return DEFAULT_CONNECTION.get();
}
Connection c = DbUpgrade.connectOrUpgrade(url, info);
if (c != null) {
return c;
......@@ -73,7 +79,14 @@ public class Driver implements java.sql.Driver {
* @return if the driver understands the URL
*/
public boolean acceptsURL(String url) {
return url != null && url.startsWith(Constants.START_URL);
if (url != null) {
if (url.startsWith(Constants.START_URL)) {
return true;
} else if (url.equals(DEFAULT_URL)) {
return DEFAULT_CONNECTION.get() != null;
}
}
return false;
}
/**
......@@ -147,4 +160,11 @@ public class Driver implements java.sql.Driver {
}
}
/**
* INTERNAL
*/
public static void setDefaultConnection(Connection c) {
DEFAULT_CONNECTION.set(c);
}
}
......@@ -71,6 +71,16 @@ public class DbSettings extends SettingsBase {
*/
public final boolean dbCloseOnExit = get("DB_CLOSE_ON_EXIT", true);
/**
* Database setting <code>DEFAULT_CONNECTION</code> (default: false).<br />
* Whether Java functions can use
* <code>DriverManager.getConnection("jdbc:default:connection")</code> to
* get a database connection. This feature is disabled by default for
* performance reasons. Please note the Oracle JDBC driver will try to
* resolve this database URL if it is loaded before the H2 driver.
*/
public boolean defaultConnection = get("DEFAULT_CONNECTION", false);
/**
* Database setting <code>DEFAULT_ESCAPE</code> (default: \).<br />
* The default escape character for LIKE comparisons. To select no escape
......
......@@ -13,6 +13,7 @@ import java.lang.reflect.Modifier;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Arrays;
import org.h2.Driver;
import org.h2.command.Parser;
import org.h2.constant.ErrorCode;
import org.h2.constant.SysProperties;
......@@ -376,10 +377,14 @@ public class FunctionAlias extends SchemaObjectBase {
}
boolean old = session.getAutoCommit();
Value identity = session.getScopeIdentity();
boolean defaultConnection = session.getDatabase().getSettings().defaultConnection;
try {
session.setAutoCommit(false);
Object returnValue;
try {
if (defaultConnection) {
Driver.setDefaultConnection(session.createConnection(columnList));
}
returnValue = method.invoke(null, params);
if (returnValue == null) {
return ValueNull.INSTANCE;
......@@ -401,6 +406,9 @@ public class FunctionAlias extends SchemaObjectBase {
} finally {
session.setScopeIdentity(identity);
session.setAutoCommit(old);
if (defaultConnection) {
Driver.setDefaultConnection(null);
}
}
}
......
......@@ -14,6 +14,7 @@ import java.math.BigDecimal;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
......@@ -47,6 +48,7 @@ public class TestFunctions extends TestBase implements AggregateFunction {
public void test() throws Exception {
deleteDb("functions");
testDefaultConnection();
testFunctionInSchema();
testGreatest();
testSource();
......@@ -65,6 +67,19 @@ public class TestFunctions extends TestBase implements AggregateFunction {
IOUtils.deleteRecursive(TEMP_DIR, true);
}
private void testDefaultConnection() throws SQLException {
Connection conn = getConnection("functions;DEFAULT_CONNECTION=TRUE");
Statement stat = conn.createStatement();
stat.execute("create alias test for \""+TestFunctions.class.getName()+".testDefaultConn\"");
stat.execute("call test()");
stat.execute("drop alias test");
conn.close();
}
public static void testDefaultConn() throws SQLException {
DriverManager.getConnection("jdbc:default:connection");
}
private void testFunctionInSchema() throws SQLException {
Connection conn = getConnection("functions");
Statement stat = conn.createStatement();
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论