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

Connection pool / DataSource: a NullPointerException was thrown when using a…

Connection pool / DataSource: a NullPointerException was thrown when using a database URL that doesn't start with "jdbc:h2:".
上级 db4ce37e
......@@ -196,7 +196,7 @@ public class JdbcConnectionPool implements DataSource, ConnectionEventListener {
*/
public Connection getConnection() throws SQLException {
long max = System.currentTimeMillis() + timeout * 1000;
while (System.currentTimeMillis() <= max) {
do {
synchronized (this) {
if (activeConnections < maxConnections) {
return getConnectionNow();
......@@ -207,7 +207,7 @@ public class JdbcConnectionPool implements DataSource, ConnectionEventListener {
// ignore
}
}
}
} while (System.currentTimeMillis() <= max);
throw new SQLException("Login timeout", "08001", 8001);
}
......
......@@ -177,7 +177,13 @@ implements XADataSource, DataSource, ConnectionPoolDataSource, Serializable, Ref
Properties info = new Properties();
info.setProperty("user", user);
info.put("password", password);
return (JdbcConnection) Driver.load().connect(url, info);
Connection conn = Driver.load().connect(url, info);
if (conn == null) {
throw new SQLException("No suitable driver found for " + url, "08001", 8001);
} else if (!(conn instanceof JdbcConnection)) {
throw new SQLException("Unsupported connection type " + conn.getClass().getName(), "08001", 8001);
}
return (JdbcConnection) conn;
}
/**
......@@ -308,7 +314,7 @@ implements XADataSource, DataSource, ConnectionPoolDataSource, Serializable, Ref
public XAConnection getXAConnection() throws SQLException {
debugCodeCall("getXAConnection");
int id = getNextId(XA_DATA_SOURCE);
return new JdbcXAConnection(factory, id, url, userName, passwordChars);
return new JdbcXAConnection(factory, id, getJdbcConnection(userName, StringUtils.cloneCharArray(passwordChars)));
}
//## Java 1.4 end ##
......@@ -326,7 +332,7 @@ implements XADataSource, DataSource, ConnectionPoolDataSource, Serializable, Ref
debugCode("getXAConnection("+quote(user)+", \"\");");
}
int id = getNextId(XA_DATA_SOURCE);
return new JdbcXAConnection(factory, id, url, user, convertToCharArray(password));
return new JdbcXAConnection(factory, id, getJdbcConnection(user, convertToCharArray(password)));
}
//## Java 1.4 end ##
......
......@@ -12,14 +12,12 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Properties;
import javax.sql.ConnectionEvent;
import javax.sql.ConnectionEventListener;
import javax.sql.XAConnection;
import javax.transaction.xa.XAException;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;
import org.h2.Driver;
import org.h2.constant.ErrorCode;
import org.h2.constant.SysProperties;
import org.h2.jdbc.JdbcConnection;
......@@ -51,7 +49,6 @@ implements XAConnection, XAResource
private static int nextTransactionId;
private JdbcDataSourceFactory factory;
private String url, user;
// This connection is kept open as long as the XAConnection is alive
private JdbcConnection physicalConn;
......@@ -66,15 +63,10 @@ implements XAConnection, XAResource
org.h2.Driver.load();
}
JdbcXAConnection(JdbcDataSourceFactory factory, int id, String url, String user, char[] password) throws SQLException {
JdbcXAConnection(JdbcDataSourceFactory factory, int id, JdbcConnection physicalConn) throws SQLException {
this.factory = factory;
setTrace(factory.getTrace(), TraceObject.XA_DATA_SOURCE, id);
this.url = url;
this.user = user;
Properties info = new Properties();
info.setProperty("user", user);
info.put("password", StringUtils.cloneCharArray(password));
physicalConn = (JdbcConnection) Driver.load().connect(url, info);
this.physicalConn = physicalConn;
}
//## Java 1.4 end ##
......@@ -422,7 +414,7 @@ implements XAConnection, XAResource
*/
//## Java 1.4 begin ##
public String toString() {
return getTraceObjectName() + ": url=" + url + " user=" + user;
return getTraceObjectName() + ": " + physicalConn;
}
private XAException convertException(SQLException e) {
......
......@@ -34,6 +34,7 @@ public class TestConnectionPool extends TestBase {
public void test() throws Exception {
deleteDb("connectionPool");
testWrongUrl();
testTimeout();
testUncommittedTransaction();
testPerformance();
......@@ -43,11 +44,22 @@ public class TestConnectionPool extends TestBase {
deleteDb("connectionPool");
}
private void testWrongUrl() throws SQLException {
JdbcConnectionPool cp = JdbcConnectionPool.create("jdbc:wrong:url", "", "");
try {
cp.getConnection();
} catch (SQLException e) {
assertEquals(8001, e.getErrorCode());
}
cp.dispose();
}
private void testTimeout() throws Exception {
String url = getURL("connectionPool", true), user = getUser(), password = getPassword();
final JdbcConnectionPool man = JdbcConnectionPool.create(url, user, password);
man.setLoginTimeout(1);
man.setMaxConnections(2);
// connection 1 (of 2)
Connection conn = man.getConnection();
Task t = new Task() {
public void call() {
......@@ -61,10 +73,13 @@ public class TestConnectionPool extends TestBase {
t.execute();
long time = System.currentTimeMillis();
try {
// connection 2 (of 1 or 2) may fail
man.getConnection();
// connection 3 (of 1 or 2) must fail
man.getConnection();
fail();
} catch (SQLException e) {
assertTrue(e.toString().toLowerCase().indexOf("timeout") >= 0);
time = System.currentTimeMillis() - time;
assertTrue("timeout after " + time + " ms", time > 1000);
} finally {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论