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

Auto-Reconnect

上级 04e8cbad
......@@ -13,7 +13,6 @@ import java.sql.Types;
import java.util.Map;
import org.h2.constant.ErrorCode;
import org.h2.engine.SessionInterface;
import org.h2.message.Message;
import org.h2.message.TraceObject;
import org.h2.tools.SimpleResultSet;
......@@ -31,8 +30,8 @@ public class JdbcArray extends TraceObject implements Array {
/**
* INTERNAL
*/
JdbcArray(SessionInterface session, JdbcConnection conn, Value value, int id) {
setTrace(session.getTrace(), TraceObject.ARRAY, id);
JdbcArray(JdbcConnection conn, Value value, int id) {
setTrace(conn.getSession().getTrace(), TraceObject.ARRAY, id);
this.conn = conn;
this.value = value;
}
......
......@@ -15,7 +15,6 @@ import java.sql.SQLException;
import org.h2.constant.ErrorCode;
import org.h2.engine.Constants;
import org.h2.engine.SessionInterface;
import org.h2.message.Message;
import org.h2.message.TraceObject;
import org.h2.util.IOUtils;
......@@ -32,8 +31,8 @@ public class JdbcBlob extends TraceObject implements Blob {
/**
* INTERNAL
*/
public JdbcBlob(SessionInterface session, JdbcConnection conn, Value value, int id) {
setTrace(session.getTrace(), TraceObject.BLOB, id);
public JdbcBlob(JdbcConnection conn, Value value, int id) {
setTrace(conn.getSession().getTrace(), TraceObject.BLOB, id);
this.conn = conn;
this.value = value;
}
......
......@@ -15,7 +15,6 @@ import java.sql.SQLException;
import org.h2.constant.ErrorCode;
import org.h2.engine.Constants;
import org.h2.engine.SessionInterface;
import org.h2.message.Message;
import org.h2.message.TraceObject;
import org.h2.util.IOUtils;
......@@ -40,8 +39,8 @@ public class JdbcClob extends TraceObject implements Clob
/**
* INTERNAL
*/
public JdbcClob(SessionInterface session, JdbcConnection conn, Value value, int id) {
setTrace(session.getTrace(), TraceObject.CLOB, id);
public JdbcClob(JdbcConnection conn, Value value, int id) {
setTrace(conn.getSession().getTrace(), TraceObject.CLOB, id);
this.conn = conn;
this.value = value;
}
......
......@@ -1256,7 +1256,7 @@ public class JdbcConnection extends TraceObject implements Connection {
*
* @throws SQLException if the connection or session is closed
*/
void checkClosed() throws SQLException {
boolean checkClosed() throws SQLException {
if (session == null) {
throw Message.getSQLException(ErrorCode.OBJECT_CLOSED);
}
......@@ -1265,11 +1265,11 @@ public class JdbcConnection extends TraceObject implements Connection {
}
if (session.isReconnectNeeded()) {
trace.debug("reconnect");
int todoInvalidatePreparedStatements;
session = session.reconnect();
trace = session.getTrace();
setTrace(trace, TraceObject.CONNECTION, getTraceId());
setTrace(session.getTrace());
return true;
}
return false;
}
String getURL() throws SQLException {
......@@ -1343,7 +1343,7 @@ public class JdbcConnection extends TraceObject implements Connection {
debugCodeAssign("Clob", TraceObject.CLOB, id, "createClob()");
checkClosed();
ValueLob v = ValueLob.createSmallLob(Value.CLOB, new byte[0]);
return new JdbcClob(session, this, v, id);
return new JdbcClob(this, v, id);
} catch (Exception e) {
throw logAndConvert(e);
}
......@@ -1360,7 +1360,7 @@ public class JdbcConnection extends TraceObject implements Connection {
debugCodeAssign("Blob", TraceObject.BLOB, id, "createClob()");
checkClosed();
ValueLob v = ValueLob.createSmallLob(Value.BLOB, new byte[0]);
return new JdbcBlob(session, this, v, id);
return new JdbcBlob(this, v, id);
} catch (Exception e) {
throw logAndConvert(e);
}
......@@ -1378,7 +1378,7 @@ public class JdbcConnection extends TraceObject implements Connection {
debugCodeAssign("NClob", TraceObject.CLOB, id, "createNClob()");
checkClosed();
ValueLob v = ValueLob.createSmallLob(Value.CLOB, new byte[0]);
return new JdbcClob(session, this, v, id);
return new JdbcClob(this, v, id);
} catch (Exception e) {
throw logAndConvert(e);
}
......@@ -1560,7 +1560,7 @@ public class JdbcConnection extends TraceObject implements Connection {
case Value.CLOB: {
if (SysProperties.RETURN_LOB_OBJECTS) {
int id = getNextId(TraceObject.CLOB);
o = new JdbcClob(session, this, v, id);
o = new JdbcClob(this, v, id);
} else {
o = v.getObject();
}
......@@ -1569,7 +1569,7 @@ public class JdbcConnection extends TraceObject implements Connection {
case Value.BLOB: {
if (SysProperties.RETURN_LOB_OBJECTS) {
int id = getNextId(TraceObject.BLOB);
o = new JdbcBlob(session, this, v, id);
o = new JdbcBlob(this, v, id);
} else {
o = v.getObject();
}
......
......@@ -10,9 +10,9 @@ import java.sql.ParameterMetaData;
import java.sql.SQLException;
import org.h2.command.CommandInterface;
import org.h2.engine.SessionInterface;
import org.h2.expression.ParameterInterface;
import org.h2.message.Message;
import org.h2.message.Trace;
import org.h2.message.TraceObject;
import org.h2.util.MathUtils;
import org.h2.util.ObjectArray;
......@@ -32,8 +32,8 @@ implements ParameterMetaData
private int paramCount;
private ObjectArray parameters;
JdbcParameterMetaData(SessionInterface session, JdbcPreparedStatement prep, CommandInterface command, int id) {
setTrace(session.getTrace(), TraceObject.PARAMETER_META_DATA, id);
JdbcParameterMetaData(Trace trace, JdbcPreparedStatement prep, CommandInterface command, int id) {
setTrace(trace, TraceObject.PARAMETER_META_DATA, id);
this.prep = prep;
this.parameters = command.getParameters();
this.paramCount = parameters.size();
......
......@@ -62,6 +62,7 @@ import java.sql.SQLXML;
*/
public class JdbcPreparedStatement extends JdbcStatement implements PreparedStatement {
private final String sql;
private CommandInterface command;
private ObjectArray batchParameters;
......@@ -69,6 +70,7 @@ public class JdbcPreparedStatement extends JdbcStatement implements PreparedStat
boolean closeWithResultSet) throws SQLException {
super(conn, resultSetType, id, closeWithResultSet);
setTrace(session.getTrace(), TraceObject.PREPARED_STATEMENT, id);
this.sql = sql;
command = conn.prepareCommand(sql, fetchSize);
}
......@@ -160,7 +162,7 @@ public class JdbcPreparedStatement extends JdbcStatement implements PreparedStat
checkClosed();
closeOldResultSet();
boolean returnsResultSet;
synchronized (session) {
synchronized (conn.getSession()) {
try {
setExecutingStatement(command);
if (command.isQuery()) {
......@@ -432,7 +434,7 @@ public class JdbcPreparedStatement extends JdbcStatement implements PreparedStat
if (x == null) {
setParameter(parameterIndex, ValueNull.INSTANCE);
} else {
Value v = DataType.convertToValue(session, x, type);
Value v = DataType.convertToValue(conn.getSession(), x, type);
setParameter(parameterIndex, v.convertTo(type));
}
} catch (Exception e) {
......@@ -1206,7 +1208,7 @@ public class JdbcPreparedStatement extends JdbcStatement implements PreparedStat
debugCodeAssign("ParameterMetaData", TraceObject.PARAMETER_META_DATA, id, "getParameterMetaData()");
}
checkClosed();
JdbcParameterMetaData meta = new JdbcParameterMetaData(session, this, command, id);
JdbcParameterMetaData meta = new JdbcParameterMetaData(session.getTrace(), this, command, id);
return meta;
} catch (Exception e) {
throw logAndConvert(e);
......@@ -1408,7 +1410,16 @@ public class JdbcPreparedStatement extends JdbcStatement implements PreparedStat
* INTERNAL
*/
public String toString() {
return getTraceObjectName() + ": " + command.toString();
return getTraceObjectName() + ": " + command;
}
boolean checkClosed() throws SQLException {
if (super.checkClosed()) {
// if the session was re-connected, re-prepare the statement
command = conn.prepareCommand(sql, fetchSize);
return true;
}
return false;
}
}
......@@ -34,7 +34,6 @@ import java.sql.SQLXML;
import org.h2.constant.ErrorCode;
import org.h2.constant.SysProperties;
import org.h2.engine.SessionInterface;
import org.h2.message.Message;
import org.h2.message.TraceObject;
import org.h2.result.ResultInterface;
......@@ -79,7 +78,6 @@ import org.h2.value.ValueTimestamp;
* </p>
*/
public class JdbcResultSet extends TraceObject implements ResultSet {
private final SessionInterface session;
private final boolean closeStatement;
private final boolean scrollable;
private ResultInterface result;
......@@ -94,8 +92,7 @@ public class JdbcResultSet extends TraceObject implements ResultSet {
JdbcResultSet(JdbcConnection conn, JdbcStatement stat, ResultInterface result, int id,
boolean closeStatement, boolean scrollable) {
this.session = conn.getSession();
setTrace(session.getTrace(), TraceObject.RESULT_SET, id);
setTrace(conn.getSession().getTrace(), TraceObject.RESULT_SET, id);
this.conn = conn;
this.stat = stat;
this.result = result;
......@@ -132,7 +129,7 @@ public class JdbcResultSet extends TraceObject implements ResultSet {
}
checkClosed();
String catalog = conn.getCatalog();
JdbcResultSetMetaData meta = new JdbcResultSetMetaData(this, null, result, catalog, session.getTrace(), id);
JdbcResultSetMetaData meta = new JdbcResultSetMetaData(this, null, result, catalog, conn.getSession().getTrace(), id);
return meta;
} catch (Exception e) {
throw logAndConvert(e);
......@@ -964,7 +961,7 @@ public class JdbcResultSet extends TraceObject implements ResultSet {
int id = getNextId(TraceObject.BLOB);
debugCodeAssign("Blob", TraceObject.BLOB, id, "getBlob(" + columnIndex + ")");
Value v = get(columnIndex);
return v == ValueNull.INSTANCE ? null : new JdbcBlob(session, conn, v, id);
return v == ValueNull.INSTANCE ? null : new JdbcBlob(conn, v, id);
} catch (Exception e) {
throw logAndConvert(e);
}
......@@ -983,7 +980,7 @@ public class JdbcResultSet extends TraceObject implements ResultSet {
int id = getNextId(TraceObject.BLOB);
debugCodeAssign("Blob", TraceObject.BLOB, id, "getBlob(" + quote(columnName) + ")");
Value v = get(columnName);
return v == ValueNull.INSTANCE ? null : new JdbcBlob(session, conn, v, id);
return v == ValueNull.INSTANCE ? null : new JdbcBlob(conn, v, id);
} catch (Exception e) {
throw logAndConvert(e);
}
......@@ -1071,7 +1068,7 @@ public class JdbcResultSet extends TraceObject implements ResultSet {
int id = getNextId(TraceObject.CLOB);
debugCodeAssign("Clob", TraceObject.CLOB, id, "getClob(" + columnIndex + ")");
Value v = get(columnIndex);
return v == ValueNull.INSTANCE ? null : new JdbcClob(session, conn, v, id);
return v == ValueNull.INSTANCE ? null : new JdbcClob(conn, v, id);
} catch (Exception e) {
throw logAndConvert(e);
}
......@@ -1090,7 +1087,7 @@ public class JdbcResultSet extends TraceObject implements ResultSet {
int id = getNextId(TraceObject.CLOB);
debugCodeAssign("Clob", TraceObject.CLOB, id, "getClob(" + quote(columnName) + ")");
Value v = get(columnName);
return v == ValueNull.INSTANCE ? null : new JdbcClob(session, conn, v, id);
return v == ValueNull.INSTANCE ? null : new JdbcClob(conn, v, id);
} catch (Exception e) {
throw logAndConvert(e);
}
......@@ -1109,7 +1106,7 @@ public class JdbcResultSet extends TraceObject implements ResultSet {
int id = getNextId(TraceObject.ARRAY);
debugCodeAssign("Clob", TraceObject.ARRAY, id, "getArray(" + columnIndex + ")");
Value v = get(columnIndex);
return v == ValueNull.INSTANCE ? null : new JdbcArray(session, conn, v, id);
return v == ValueNull.INSTANCE ? null : new JdbcArray(conn, v, id);
} catch (Exception e) {
throw logAndConvert(e);
}
......@@ -1128,7 +1125,7 @@ public class JdbcResultSet extends TraceObject implements ResultSet {
int id = getNextId(TraceObject.ARRAY);
debugCodeAssign("Clob", TraceObject.ARRAY, id, "getArray(" + quote(columnName) + ")");
Value v = get(columnName);
return v == ValueNull.INSTANCE ? null : new JdbcArray(session, conn, v, id);
return v == ValueNull.INSTANCE ? null : new JdbcArray(conn, v, id);
} catch (Exception e) {
throw logAndConvert(e);
}
......@@ -2005,12 +2002,14 @@ public class JdbcResultSet extends TraceObject implements ResultSet {
if (isDebugEnabled()) {
debugCode("updateObject("+columnIndex+", x, "+scale+");");
}
update(columnIndex, DataType.convertToValue(session, x, Value.UNKNOWN));
update(columnIndex, convertToUnknownValue(x));
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
* Updates a column in the current or insert row.
*
......@@ -2024,7 +2023,7 @@ public class JdbcResultSet extends TraceObject implements ResultSet {
if (isDebugEnabled()) {
debugCode("updateObject("+quote(columnName)+", x, "+scale+");");
}
update(columnName, DataType.convertToValue(session, x, Value.UNKNOWN));
update(columnName, convertToUnknownValue(x));
} catch (Exception e) {
throw logAndConvert(e);
}
......@@ -2042,7 +2041,7 @@ public class JdbcResultSet extends TraceObject implements ResultSet {
if (isDebugEnabled()) {
debugCode("updateObject("+columnIndex+", x);");
}
update(columnIndex, DataType.convertToValue(session, x, Value.UNKNOWN));
update(columnIndex, convertToUnknownValue(x));
} catch (Exception e) {
throw logAndConvert(e);
}
......@@ -2060,7 +2059,7 @@ public class JdbcResultSet extends TraceObject implements ResultSet {
if (isDebugEnabled()) {
debugCode("updateObject("+quote(columnName)+", x);");
}
update(columnName, DataType.convertToValue(session, x, Value.UNKNOWN));
update(columnName, convertToUnknownValue(x));
} catch (Exception e) {
throw logAndConvert(e);
}
......@@ -2393,7 +2392,7 @@ public class JdbcResultSet extends TraceObject implements ResultSet {
try {
debugCodeCall("getConcurrency");
checkClosed();
UpdatableRow row = new UpdatableRow(conn, result, session);
UpdatableRow row = new UpdatableRow(conn, result);
return row.isUpdatable() ? ResultSet.CONCUR_UPDATABLE : ResultSet.CONCUR_READ_ONLY;
} catch (Exception e) {
throw logAndConvert(e);
......@@ -2909,7 +2908,7 @@ public class JdbcResultSet extends TraceObject implements ResultSet {
// =============================================================
private UpdatableRow getUpdatableRow() throws SQLException {
UpdatableRow row = new UpdatableRow(conn, result, session);
UpdatableRow row = new UpdatableRow(conn, result);
if (!row.isUpdatable()) {
throw Message.getSQLException(ErrorCode.RESULT_SET_NOT_UPDATABLE);
}
......@@ -3273,7 +3272,7 @@ public class JdbcResultSet extends TraceObject implements ResultSet {
int id = getNextId(TraceObject.CLOB);
debugCodeAssign("NClob", TraceObject.CLOB, id, "getNClob(" + columnIndex + ")");
Value v = get(columnIndex);
return v == ValueNull.INSTANCE ? null : new JdbcClob(session, conn, v, id);
return v == ValueNull.INSTANCE ? null : new JdbcClob(conn, v, id);
} catch (Exception e) {
throw logAndConvert(e);
}
......@@ -3293,7 +3292,7 @@ public class JdbcResultSet extends TraceObject implements ResultSet {
int id = getNextId(TraceObject.CLOB);
debugCodeAssign("NClob", TraceObject.CLOB, id, "getNClob(" + columnName + ")");
Value v = get(columnName);
return v == ValueNull.INSTANCE ? null : new JdbcClob(session, conn, v, id);
return v == ValueNull.INSTANCE ? null : new JdbcClob(conn, v, id);
} catch (Exception e) {
throw logAndConvert(e);
}
......@@ -3517,4 +3516,8 @@ public class JdbcResultSet extends TraceObject implements ResultSet {
}
}
private Value convertToUnknownValue(Object x) throws SQLException {
return DataType.convertToValue(conn.getSession(), x, Value.UNKNOWN);
}
}
......@@ -6,7 +6,6 @@
*/
package org.h2.jdbc;
import java.sql.BatchUpdateException;
import java.sql.Connection;
import java.sql.ResultSet;
......@@ -847,13 +846,19 @@ public class JdbcStatement extends TraceObject implements Statement {
/**
* Check if the statement is closed.
*
* @return true if a reconnect was required
* @throws SQLException if it is closed
*/
void checkClosed() throws SQLException {
boolean checkClosed() throws SQLException {
if (conn == null) {
throw Message.getSQLException(ErrorCode.OBJECT_CLOSED);
}
conn.checkClosed();
if (conn.checkClosed()) {
session = conn.getSession();
setTrace(session.getTrace());
return true;
}
return false;
}
/**
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论