提交 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 ...@@ -18,7 +18,10 @@ Change Log
<h1>Change Log</h1> <h1>Change Log</h1>
<h2>Next Version (unreleased)</h2> <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 </li><li>DatabaseMetaData.getProcedureColumns returned the wrong data
(it also returned the Connection parameter if there was any). (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), </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; ...@@ -32,6 +32,9 @@ import org.h2.upgrade.DbUpgrade;
public class Driver implements java.sql.Driver { public class Driver implements java.sql.Driver {
private static final Driver INSTANCE = new 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; private static volatile boolean registered;
static { static {
...@@ -55,6 +58,9 @@ public class Driver implements java.sql.Driver { ...@@ -55,6 +58,9 @@ public class Driver implements java.sql.Driver {
if (!acceptsURL(url)) { if (!acceptsURL(url)) {
return null; return null;
} }
if (url.equals(DEFAULT_URL)) {
return DEFAULT_CONNECTION.get();
}
Connection c = DbUpgrade.connectOrUpgrade(url, info); Connection c = DbUpgrade.connectOrUpgrade(url, info);
if (c != null) { if (c != null) {
return c; return c;
...@@ -73,7 +79,14 @@ public class Driver implements java.sql.Driver { ...@@ -73,7 +79,14 @@ public class Driver implements java.sql.Driver {
* @return if the driver understands the URL * @return if the driver understands the URL
*/ */
public boolean acceptsURL(String 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 { ...@@ -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 { ...@@ -71,6 +71,16 @@ public class DbSettings extends SettingsBase {
*/ */
public final boolean dbCloseOnExit = get("DB_CLOSE_ON_EXIT", true); 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 /> * Database setting <code>DEFAULT_ESCAPE</code> (default: \).<br />
* The default escape character for LIKE comparisons. To select no escape * The default escape character for LIKE comparisons. To select no escape
......
...@@ -13,6 +13,7 @@ import java.lang.reflect.Modifier; ...@@ -13,6 +13,7 @@ import java.lang.reflect.Modifier;
import java.sql.Connection; import java.sql.Connection;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import org.h2.Driver;
import org.h2.command.Parser; import org.h2.command.Parser;
import org.h2.constant.ErrorCode; import org.h2.constant.ErrorCode;
import org.h2.constant.SysProperties; import org.h2.constant.SysProperties;
...@@ -376,10 +377,14 @@ public class FunctionAlias extends SchemaObjectBase { ...@@ -376,10 +377,14 @@ public class FunctionAlias extends SchemaObjectBase {
} }
boolean old = session.getAutoCommit(); boolean old = session.getAutoCommit();
Value identity = session.getScopeIdentity(); Value identity = session.getScopeIdentity();
boolean defaultConnection = session.getDatabase().getSettings().defaultConnection;
try { try {
session.setAutoCommit(false); session.setAutoCommit(false);
Object returnValue; Object returnValue;
try { try {
if (defaultConnection) {
Driver.setDefaultConnection(session.createConnection(columnList));
}
returnValue = method.invoke(null, params); returnValue = method.invoke(null, params);
if (returnValue == null) { if (returnValue == null) {
return ValueNull.INSTANCE; return ValueNull.INSTANCE;
...@@ -401,6 +406,9 @@ public class FunctionAlias extends SchemaObjectBase { ...@@ -401,6 +406,9 @@ public class FunctionAlias extends SchemaObjectBase {
} finally { } finally {
session.setScopeIdentity(identity); session.setScopeIdentity(identity);
session.setAutoCommit(old); session.setAutoCommit(old);
if (defaultConnection) {
Driver.setDefaultConnection(null);
}
} }
} }
......
...@@ -14,6 +14,7 @@ import java.math.BigDecimal; ...@@ -14,6 +14,7 @@ import java.math.BigDecimal;
import java.sql.Blob; import java.sql.Blob;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DatabaseMetaData; import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
...@@ -47,6 +48,7 @@ public class TestFunctions extends TestBase implements AggregateFunction { ...@@ -47,6 +48,7 @@ public class TestFunctions extends TestBase implements AggregateFunction {
public void test() throws Exception { public void test() throws Exception {
deleteDb("functions"); deleteDb("functions");
testDefaultConnection();
testFunctionInSchema(); testFunctionInSchema();
testGreatest(); testGreatest();
testSource(); testSource();
...@@ -65,6 +67,19 @@ public class TestFunctions extends TestBase implements AggregateFunction { ...@@ -65,6 +67,19 @@ public class TestFunctions extends TestBase implements AggregateFunction {
IOUtils.deleteRecursive(TEMP_DIR, true); 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 { private void testFunctionInSchema() throws SQLException {
Connection conn = getConnection("functions"); Connection conn = getConnection("functions");
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论