Eliminate the prepared statement caching as it's not useful since we can't use

server side prepared statement caching and it's causing things to break when
for some reason the prepared statements are being closed (perahps automatically
by the new MySQL deriver) and then being reused.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@1834 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2006-04-28 21:30:35 +00:00
parent e8cb1fb2d4
commit 38f09416b7
2 changed files with 40 additions and 127 deletions
+4 -13
View File
@@ -55,19 +55,10 @@ public class Cursor<V>
if (_result == null) { if (_result == null) {
if (_qbeObject != null) { if (_qbeObject != null) {
PreparedStatement qbeStmt; PreparedStatement qbeStmt = _conn.prepareStatement(_query);
synchronized (_table.preparedStmtHash) { _table.bindQueryVariables(qbeStmt, _qbeObject, _qbeMask);
qbeStmt = _table.preparedStmtHash.get(_query); _result = qbeStmt.executeQuery();
if (qbeStmt == null) { _stmt = qbeStmt;
qbeStmt = _conn.prepareStatement(_query);
_table.preparedStmtHash.put(_query, qbeStmt);
}
}
synchronized (qbeStmt) {
_table.bindQueryVariables(qbeStmt, _qbeObject, _qbeMask);
_result = qbeStmt.executeQuery();
qbeStmt.clearParameters();
}
} else { } else {
if (_stmt == null) { if (_stmt == null) {
_stmt = _conn.createStatement(); _stmt = _conn.createStatement();
+36 -114
View File
@@ -213,19 +213,16 @@ public class Table<T>
public synchronized void insert (Connection conn, T obj) public synchronized void insert (Connection conn, T obj)
throws SQLException throws SQLException
{ {
checkUpdateConnection(conn); String sql = "insert into " + name +
if (insertStmt == null) { " (" + listOfFields + ") values (?";
String sql = "insert into " + name + " (" for (int i = 1; i < nColumns; i++) {
+ listOfFields + ") values (?"; sql += ",?";
for (int i = 1; i < nColumns; i++) {
sql += ",?";
}
sql += ")";
insertStmt = conn.prepareStatement(sql);
} }
sql += ")";
PreparedStatement insertStmt = conn.prepareStatement(sql);
bindUpdateVariables(insertStmt, obj, null); bindUpdateVariables(insertStmt, obj, null);
insertStmt.executeUpdate(); insertStmt.executeUpdate();
insertStmt.clearParameters(); insertStmt.close();
} }
/** /**
@@ -238,22 +235,19 @@ public class Table<T>
public synchronized void insert (Connection conn, T[] objects) public synchronized void insert (Connection conn, T[] objects)
throws SQLException throws SQLException
{ {
checkUpdateConnection(conn); String sql = "insert into " + name +
if (insertStmt == null) { " (" + listOfFields + ") values (?";
String sql = "insert into " + name + " (" for (int i = 1; i < nColumns; i++) {
+ listOfFields + ") values (?"; sql += ",?";
for (int i = 1; i < nColumns; i++) {
sql += ",?";
}
sql += ")";
insertStmt = conn.prepareStatement(sql);
} }
sql += ")";
PreparedStatement insertStmt = conn.prepareStatement(sql);
for (int i = 0; i < objects.length; i++) { for (int i = 0; i < objects.length; i++) {
bindUpdateVariables(insertStmt, objects[i], null); bindUpdateVariables(insertStmt, objects[i], null);
insertStmt.addBatch(); insertStmt.addBatch();
} }
insertStmt.executeBatch(); insertStmt.executeBatch();
insertStmt.clearParameters(); insertStmt.close();
} }
/** /**
@@ -298,32 +292,17 @@ public class Table<T>
throws SQLException throws SQLException
{ {
int nUpdated = 0; int nUpdated = 0;
checkUpdateConnection(conn); String sql = "update " + name + " set " +
PreparedStatement ustmt; (mask != null ? buildListOfAssignments(mask) : listOfAssignments) +
// if we have a field mask, we need to create a custom update statement buildUpdateWhere();
if (mask != null) { PreparedStatement ustmt = conn.prepareStatement(sql);
String sql = "update " + name + " set " +
buildListOfAssignments(mask)
+ buildUpdateWhere();
ustmt = conn.prepareStatement(sql);
} else {
// otherwise we can use our "full-object-update" statement
if (updateStmt == null) {
String sql = "update " + name + " set " + listOfAssignments
+ buildUpdateWhere();
updateStmt = conn.prepareStatement(sql);
}
ustmt = updateStmt;
}
// bind the update variables
int column = bindUpdateVariables(ustmt, obj, mask); int column = bindUpdateVariables(ustmt, obj, mask);
// bind the keys
for (int i = 0; i < primaryKeys.length; i++) { for (int i = 0; i < primaryKeys.length; i++) {
int fidx = primaryKeyIndices[i]; int fidx = primaryKeyIndices[i];
fields[fidx].bindVariable(ustmt, obj, column+i+1); fields[fidx].bindVariable(ustmt, obj, column+i+1);
} }
nUpdated = ustmt.executeUpdate(); nUpdated = ustmt.executeUpdate();
ustmt.clearParameters(); ustmt.close();
return nUpdated; return nUpdated;
} }
@@ -344,13 +323,11 @@ public class Table<T>
throw new IllegalStateException( throw new IllegalStateException(
"No primary key for table " + name + "."); "No primary key for table " + name + ".");
} }
int nUpdated = 0; int nUpdated = 0;
checkUpdateConnection(conn); String sql = "update " + name + " set " + listOfAssignments +
if (updateStmt == null) { buildUpdateWhere();
String sql = "update " + name + " set " + listOfAssignments PreparedStatement updateStmt = conn.prepareStatement(sql);
+ buildUpdateWhere();
updateStmt = conn.prepareStatement(sql);
}
for (int i = 0; i < objects.length; i++) { for (int i = 0; i < objects.length; i++) {
int column = bindUpdateVariables(updateStmt, objects[i], null); int column = bindUpdateVariables(updateStmt, objects[i], null);
for (int j = 0; j < primaryKeys.length; j++) { for (int j = 0; j < primaryKeys.length; j++) {
@@ -364,7 +341,7 @@ public class Table<T>
for (int k = 0; k < rc.length; k++) { for (int k = 0; k < rc.length; k++) {
nUpdated += rc[k]; nUpdated += rc[k];
} }
updateStmt.clearParameters(); updateStmt.close();
return nUpdated; return nUpdated;
} }
@@ -381,20 +358,17 @@ public class Table<T>
"No primary key for table " + name + "."); "No primary key for table " + name + ".");
} }
int nDeleted = 0; int nDeleted = 0;
checkUpdateConnection(conn); String sql = "delete from " + name +
if (deleteStmt == null) { " where " + primaryKeys[0] + " = ?";
String sql = "delete from " + name + for (int i = 1; i < primaryKeys.length; i++) {
" where " + primaryKeys[0] + " = ?"; sql += " and " + primaryKeys[i] + " = ?";
for (int i = 1; i < primaryKeys.length; i++) {
sql += " and " + primaryKeys[i] + " = ?";
}
deleteStmt = conn.prepareStatement(sql);
} }
PreparedStatement deleteStmt = conn.prepareStatement(sql);
for (int i = 0; i < primaryKeys.length; i++) { for (int i = 0; i < primaryKeys.length; i++) {
fields[primaryKeyIndices[i]].bindVariable(deleteStmt, obj,i+1); fields[primaryKeyIndices[i]].bindVariable(deleteStmt, obj,i+1);
} }
nDeleted = deleteStmt.executeUpdate(); nDeleted = deleteStmt.executeUpdate();
deleteStmt.clearParameters(); deleteStmt.close();
return nDeleted; return nDeleted;
} }
@@ -413,15 +387,12 @@ public class Table<T>
"No primary key for table " + name + "."); "No primary key for table " + name + ".");
} }
int nDeleted = 0; int nDeleted = 0;
checkUpdateConnection(conn); String sql = "delete from " + name +
if (deleteStmt == null) { " where " + primaryKeys[0] + " = ?";
String sql = "delete from " + name + for (int i = 1; i < primaryKeys.length; i++) {
" where " + primaryKeys[0] + " = ?"; sql += " and " + primaryKeys[i] + " = ?";
for (int i = 1; i < primaryKeys.length; i++) {
sql += " and " + primaryKeys[i] + " = ?";
}
deleteStmt = conn.prepareStatement(sql);
} }
PreparedStatement deleteStmt = conn.prepareStatement(sql);
for (int i = 0; i < objects.length; i++) { for (int i = 0; i < objects.length; i++) {
for (int j = 0; j < primaryKeys.length; j++) { for (int j = 0; j < primaryKeys.length; j++) {
fields[primaryKeyIndices[j]].bindVariable( fields[primaryKeyIndices[j]].bindVariable(
@@ -433,7 +404,7 @@ public class Table<T>
for (int k = 0; k < rc.length; k++) { for (int k = 0; k < rc.length; k++) {
nDeleted += rc[k]; nDeleted += rc[k];
} }
deleteStmt.clearParameters(); deleteStmt.close();
return nDeleted; return nDeleted;
} }
@@ -497,46 +468,6 @@ public class Table<T>
} }
} }
/**
* Flushes our insert, update and delete statements if the supplied
* connection differs from the one used to prepare them.
*/
protected void checkUpdateConnection (Connection conn)
throws SQLException
{
if (_updateConn != conn) {
if (insertStmt != null) {
JDBCUtil.close(insertStmt);
insertStmt = null;
}
if (updateStmt != null) {
JDBCUtil.close(updateStmt);
updateStmt = null;
}
if (deleteStmt != null) {
JDBCUtil.close(deleteStmt);
deleteStmt = null;
}
_updateConn = conn;
}
}
/**
* Flushes our cached Cursor prepared statements if the supplied connection
* differes from the one used to prepare them.
*/
protected void checkReadConnection (Connection conn)
throws SQLException
{
if (_readConn != conn) {
for (PreparedStatement pstmt : preparedStmtHash.values()) {
JDBCUtil.close(pstmt);
}
preparedStmtHash.clear();
_readConn = conn;
}
}
protected final String convertName (String name) protected final String convertName (String name)
{ {
if (mixedCaseConvert) { if (mixedCaseConvert) {
@@ -957,12 +888,6 @@ public class Table<T>
protected String primaryKeys[]; protected String primaryKeys[];
protected int primaryKeyIndices[]; protected int primaryKeyIndices[];
protected Connection _updateConn, _readConn;
protected PreparedStatement updateStmt;
protected PreparedStatement deleteStmt;
protected PreparedStatement insertStmt;
protected Constructor<T> constructor; protected Constructor<T> constructor;
protected static Method setBypass; protected static Method setBypass;
@@ -970,9 +895,6 @@ public class Table<T>
protected static final Object[] bypassFlag = { new Boolean(true) }; protected static final Object[] bypassFlag = { new Boolean(true) };
protected static final Object[] constructorArgs = {}; protected static final Object[] constructorArgs = {};
protected Hashtable<String,PreparedStatement> preparedStmtHash =
new Hashtable<String,PreparedStatement>();
// used to identify byte[] fields // used to identify byte[] fields
protected static final byte[] BYTE_PROTO = new byte[0]; protected static final byte[] BYTE_PROTO = new byte[0];