You are viewing a plain text version of this content. The canonical link for it is here.
Posted to slide-dev@jakarta.apache.org by re...@apache.org on 2001/03/17 19:39:25 UTC
cvs commit: jakarta-slide/src/stores/slidestore/reference JDBCContentStore.java JDBCDescriptorsStore.java
remm 01/03/17 10:39:25
Modified: src/stores/slidestore/reference JDBCContentStore.java
JDBCDescriptorsStore.java
Log:
- Close statements after excuting them.
Thanks to Jiantao Pan and Rama Kurapati for the reports.
Revision Changes Path
1.2 +52 -7 jakarta-slide/src/stores/slidestore/reference/JDBCContentStore.java
Index: JDBCContentStore.java
===================================================================
RCS file: /home/cvs/jakarta-slide/src/stores/slidestore/reference/JDBCContentStore.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- JDBCContentStore.java 2001/02/08 07:54:20 1.1
+++ JDBCContentStore.java 2001/03/17 18:39:24 1.2
@@ -1,7 +1,7 @@
/*
- * $Header: /home/cvs/jakarta-slide/src/stores/slidestore/reference/JDBCContentStore.java,v 1.1 2001/02/08 07:54:20 remm Exp $
- * $Revision: 1.1 $
- * $Date: 2001/02/08 07:54:20 $
+ * $Header: /home/cvs/jakarta-slide/src/stores/slidestore/reference/JDBCContentStore.java,v 1.2 2001/03/17 18:39:24 remm Exp $
+ * $Revision: 1.2 $
+ * $Date: 2001/03/17 18:39:24 $
*
* ====================================================================
*
@@ -89,7 +89,7 @@
* JDBC 2.0 compliant implementation of ContentStore.
*
* @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
- * @version $Revision: 1.1 $
+ * @version $Revision: 1.2 $
*/
public class JDBCContentStore extends AbstractSimpleService
implements ContentStore {
@@ -186,12 +186,15 @@
} catch (SQLException e) {
throw new ServiceConnectionFailedException(this, e);
}
+ Statement statement = null;
try {
- Statement statement = connection.createStatement();
+ statement = connection.createStatement();
String s = "create table revisioncontent(uri varchar(65536), "
+ "xnumber varchar(20), content LONGVARBINARY)";
statement.execute(s);
} catch (SQLException e) {
+ } finally {
+ closeStatement(statement);
}
}
@@ -260,6 +263,7 @@
Statement statement = connection.createStatement();
String s = "drop table revisioncontent";
statement.execute(s);
+ statement.close();
disconnect();
} catch (SQLException e) {
throw new ServiceResetFailedException(this, e.getMessage());
@@ -356,17 +360,22 @@
revisionDescriptor.getRevisionNumber().toString();
try {
+
PreparedStatement selectStatement = connection.prepareStatement
("select * from revisioncontent where uri = ? and "
+ "xnumber = ?");
selectStatement.setString(1, revisionUri);
selectStatement.setString(2, revisionNumber);
ResultSet rs = selectStatement.executeQuery();
+
if (!rs.next()) {
+ rs.close();
+ selectStatement.close();
throw new RevisionNotFoundException
(uri.toString(),
revisionDescriptor.getRevisionNumber());
}
+
InputStream is = rs.getBinaryStream(REVISION_CONTENT);
if (is == null) {
throw new RevisionNotFoundException
@@ -378,6 +387,13 @@
result = new NodeRevisionContent();
result.setContent(reader);
result.setContent(is);
+
+ // Don't close the statement or the result set here (because
+ // otherwise the is and the reader returned would be closed).
+ // If this proves to be a problem, then the binary content of the
+ // resource must either be buffer to the disk or to memory.
+ // FIXME ?
+
} catch (SQLException e) {
throw new ServiceAccessException(this, e.getMessage());
} catch (Exception e) {
@@ -406,21 +422,25 @@
String revisionNumber =
revisionDescriptor.getRevisionNumber().toString();
long contentLength = revisionDescriptor.getContentLength();
+ PreparedStatement selectStatement = null;
try {
- PreparedStatement selectStatement = connection.prepareStatement
+ selectStatement = connection.prepareStatement
("select * from revisioncontent where uri = ? and "
+ "xnumber = ?");
selectStatement.setString(1, revisionUri);
selectStatement.setString(2, revisionNumber);
ResultSet rs = selectStatement.executeQuery();
if (rs.next()) {
+ rs.close();
throw new RevisionAlreadyExistException
(uri.toString(),
revisionDescriptor.getRevisionNumber());
}
+ rs.close();
+
storeContent(revisionUri, revisionNumber, revisionDescriptor,
revisionContent);
@@ -433,6 +453,8 @@
} catch (Exception e) {
e.printStackTrace();
throw new ServiceAccessException(this, e.getMessage());
+ } finally {
+ closeStatement(selectStatement);
}
}
@@ -453,21 +475,26 @@
String revisionUri = uri.toString();
String revisionNumber =
revisionDescriptor.getRevisionNumber().toString();
+ PreparedStatement selectStatement = null;
try {
- PreparedStatement selectStatement = connection.prepareStatement
+ selectStatement = connection.prepareStatement
("select * from revisioncontent where uri = ? and "
+ "xnumber = ?");
selectStatement.setString(1, revisionUri);
selectStatement.setString(2, revisionNumber);
ResultSet rs = selectStatement.executeQuery();
if (!rs.next()) {
+ rs.close();
+ selectStatement.close();
throw new RevisionNotFoundException
(uri.toString(),
revisionDescriptor.getRevisionNumber());
}
+ rs.close();
+
removeContent(revisionUri, revisionNumber);
storeContent(revisionUri, revisionNumber, revisionDescriptor,
revisionContent);
@@ -482,6 +509,8 @@
} catch (Exception e) {
e.printStackTrace();
throw new ServiceAccessException(this, e.getMessage());
+ } finally {
+ closeStatement(selectStatement);
}
}
@@ -593,6 +622,8 @@
}
+ insertStatement.close();
+
}
@@ -607,7 +638,21 @@
deleteStatement.setString(1, revisionUri);
deleteStatement.setString(2, revisionNumber);
deleteStatement.executeUpdate();
+ deleteStatement.close();
+ }
+
+
+ /**
+ * Close specified statement.
+ */
+ protected void closeStatement(Statement statement) {
+ if (statement != null) {
+ try {
+ statement.close();
+ } catch (SQLException e) {
+ }
+ }
}
1.10 +136 -31 jakarta-slide/src/stores/slidestore/reference/JDBCDescriptorsStore.java
Index: JDBCDescriptorsStore.java
===================================================================
RCS file: /home/cvs/jakarta-slide/src/stores/slidestore/reference/JDBCDescriptorsStore.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- JDBCDescriptorsStore.java 2001/02/25 07:29:12 1.9
+++ JDBCDescriptorsStore.java 2001/03/17 18:39:24 1.10
@@ -1,7 +1,7 @@
/*
- * $Header: /home/cvs/jakarta-slide/src/stores/slidestore/reference/JDBCDescriptorsStore.java,v 1.9 2001/02/25 07:29:12 remm Exp $
- * $Revision: 1.9 $
- * $Date: 2001/02/25 07:29:12 $
+ * $Header: /home/cvs/jakarta-slide/src/stores/slidestore/reference/JDBCDescriptorsStore.java,v 1.10 2001/03/17 18:39:24 remm Exp $
+ * $Revision: 1.10 $
+ * $Date: 2001/03/17 18:39:24 $
*
* ====================================================================
*
@@ -84,7 +84,7 @@
* JDBC 1.0 and 2.0 compliant store implementation.
*
* @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
- * @version $Revision: 1.9 $
+ * @version $Revision: 1.10 $
*/
public class JDBCDescriptorsStore
@@ -259,8 +259,11 @@
throw new ServiceConnectionFailedException(this, e);
}
+ Statement statement = null;
+
try {
- Statement statement = connection.createStatement();
+
+ statement = connection.createStatement();
String s = "create table objects(uri varchar(65536) "
+ " primary key, classname varchar(4096))";
@@ -314,7 +317,11 @@
+ "namespace varchar(4096), type varchar(100), protected int)";
statement.execute(s);
+ statement.close();
+
} catch (SQLException e) {
+ } finally {
+ closeStatement(statement);
}
}
@@ -385,10 +392,11 @@
*/
public synchronized void reset()
throws ServiceResetFailedException {
+ Statement statement = null;
try {
connectIfNeeded();
- Statement statement = connection.createStatement();
+ statement = connection.createStatement();
String s = null;
s = "drop table objects";
@@ -427,6 +435,7 @@
s = "drop table property";
statement.execute(s);
+ statement.close();
disconnect();
} catch (SQLException e) {
throw new ServiceResetFailedException(this, e.getMessage());
@@ -436,6 +445,8 @@
throw new ServiceResetFailedException(this, e.getMessage());
} catch (ServiceDisconnectionFailedException e) {
throw new ServiceResetFailedException(this, e.getMessage());
+ } finally {
+ closeStatement(statement);
}
}
@@ -518,10 +529,11 @@
throws ServiceAccessException, ObjectNotFoundException {
ObjectNode result = null;
+ Statement statement = null;
try {
- Statement statement = connection.createStatement();
+ statement = connection.createStatement();
String s = "select * from objects where uri='" + uri + "'";
statement.execute(s);
@@ -599,8 +611,12 @@
}
+ res.close();
+
} catch (SQLException e) {
throw new ServiceAccessException(this, e);
+ } finally {
+ closeStatement(statement);
}
return result;
}
@@ -616,8 +632,10 @@
public void storeObject(Uri uri, ObjectNode object)
throws ServiceAccessException, ObjectNotFoundException {
+ Statement statement = null;
+
try {
- Statement statement = connection.createStatement();
+ statement = connection.createStatement();
String s = "select * from objects where uri='" + uri + "'";
@@ -663,8 +681,12 @@
statement.execute(s);
}
+ res.close();
+
} catch (SQLException e) {
throw new ServiceAccessException(this, e);
+ } finally {
+ closeStatement(statement);
}
}
@@ -682,11 +704,13 @@
public void createObject(Uri uri, ObjectNode object)
throws ServiceAccessException, ObjectAlreadyExistsException {
+ Statement statement = null;
+
try {
String className = object.getClass().getName();
- Statement statement = connection.createStatement();
+ statement = connection.createStatement();
String s = "select * from objects where uri='" + uri + "'";
@@ -730,8 +754,12 @@
statement.execute(s);
}
+ res.close();
+
} catch (SQLException e) {
throw new ServiceAccessException(this, e);
+ } finally {
+ closeStatement(statement);
}
}
@@ -747,8 +775,10 @@
public void removeObject(Uri uri, ObjectNode object)
throws ServiceAccessException, ObjectNotFoundException {
+ Statement statement = null;
+
try {
- Statement statement = connection.createStatement();
+ statement = connection.createStatement();
String s = null;
// Removing object
@@ -768,6 +798,7 @@
// Removing links
s = "delete from links where link='" + object.getUri() + "'";
statement.execute(s);
+
} catch (SQLException e) {
throw new ServiceAccessException(this, e);
}
@@ -783,8 +814,10 @@
public void grantPermission(Uri uri, NodePermission permission)
throws ServiceAccessException {
+ Statement statement = null;
+
try {
- Statement statement = connection.createStatement();
+ statement = connection.createStatement();
int inheritable = 0;
if (permission.isInheritable()) {
@@ -802,9 +835,10 @@
+ permission.getActionUri()
+ "', " + inheritable + ", " + negative + ")";
statement.execute(s);
-
} catch (SQLException e) {
throw new ServiceAccessException(this, e);
+ } finally {
+ closeStatement(statement);
}
}
@@ -819,8 +853,10 @@
public void revokePermission(Uri uri, NodePermission permission)
throws ServiceAccessException {
+ Statement statement = null;
+
try {
- Statement statement = connection.createStatement();
+ statement = connection.createStatement();
int inheritable = 0;
if (permission.isInheritable()) {
@@ -834,9 +870,10 @@
+ permission.getActionUri() + "' and inheritable="
+ inheritable;
statement.execute(s);
-
} catch (SQLException e) {
throw new ServiceAccessException(this, e);
+ } finally {
+ closeStatement(statement);
}
}
@@ -851,14 +888,17 @@
public void revokePermissions(Uri uri)
throws ServiceAccessException {
+ Statement statement = null;
+
try {
- Statement statement = connection.createStatement();
+ statement = connection.createStatement();
String s = "delete from permissions where object='" + uri + "'";
statement.execute(s);
-
} catch (SQLException e) {
throw new ServiceAccessException(this, e);
+ } finally {
+ closeStatement(statement);
}
}
@@ -874,9 +914,10 @@
throws ServiceAccessException {
Vector permissionVector = new Vector();
+ Statement statement = null;
try {
- Statement statement = connection.createStatement();
+ statement = connection.createStatement();
String s = "select * from permissions where object='" + uri + "'";
statement.execute(s);
@@ -899,8 +940,11 @@
permissionVector.addElement(permission);
}
+ res.close();
} catch (SQLException e) {
throw new ServiceAccessException(this, e);
+ } finally {
+ closeStatement(statement);
}
return permissionVector.elements();
@@ -916,8 +960,10 @@
public void putLock(Uri uri, NodeLock lock)
throws ServiceAccessException {
+ Statement statement = null;
+
try {
- Statement statement = connection.createStatement();
+ statement = connection.createStatement();
int inheritable = 0;
if (lock.isInheritable()) {
@@ -936,9 +982,10 @@
+ lock.getExpirationDate().getTime() + "', "
+ inheritable + ", " + exclusive + ")";
statement.execute(s);
-
} catch (SQLException e) {
throw new ServiceAccessException(this, e);
+ } finally {
+ closeStatement(statement);
}
}
@@ -954,9 +1001,11 @@
public void renewLock(Uri uri, NodeLock lock)
throws ServiceAccessException, LockTokenNotFoundException {
+ Statement statement = null;
+
try {
- Statement statement = connection.createStatement();
+ statement = connection.createStatement();
int inheritable = 0;
if (lock.isInheritable()) {
@@ -982,6 +1031,8 @@
} catch (SQLException e) {
throw new ServiceAccessException(this, e);
+ } finally {
+ closeStatement(statement);
}
}
@@ -997,9 +1048,11 @@
public void removeLock(Uri uri, NodeLock lock)
throws ServiceAccessException, LockTokenNotFoundException {
+ Statement statement = null;
+
try {
- Statement statement = connection.createStatement();
+ statement = connection.createStatement();
int inheritable = 0;
if (lock.isInheritable()) {
@@ -1013,6 +1066,8 @@
} catch (SQLException e) {
throw new ServiceAccessException(this, e);
+ } finally {
+ closeStatement(statement);
}
}
@@ -1044,10 +1099,11 @@
throws ServiceAccessException {
Vector lockVector = new Vector();
+ Statement statement = null;
try {
- Statement statement = connection.createStatement();
+ statement = connection.createStatement();
String s = null;
s = "select * from locks where object='" + uri + "'";
statement.execute(s);
@@ -1073,8 +1129,12 @@
lockVector.addElement(lock);
}
+ res.close();
+
} catch (SQLException e) {
throw new ServiceAccessException(this, e);
+ } finally {
+ closeStatement(statement);
}
return lockVector.elements();
}
@@ -1092,9 +1152,11 @@
throws ServiceAccessException, RevisionDescriptorNotFoundException {
NodeRevisionDescriptors revisionDescriptors = null;
+ Statement statement = null;
+ Statement statement2 = null;
try {
- Statement statement = connection.createStatement();
+ statement = connection.createStatement();
String s = null;
ResultSet res = null;
@@ -1140,7 +1202,7 @@
statement.execute(s);
res = statement.getResultSet();
- Statement statement2 = connection.createStatement();
+ statement2 = connection.createStatement();
while(res.next()) {
String currentRevisionNumber = res.getString(REVISION_NUMBER);
@@ -1158,14 +1220,21 @@
branches.put(new NodeRevisionNumber(currentRevisionNumber),
childList);
+
+ res2.close();
}
revisionDescriptors = new NodeRevisionDescriptors
(uri.toString(), initialRevision, workingRevisions,
latestRevisionNumbers, branches, isVersioned);
+ res.close();
+
} catch (SQLException e) {
throw new ServiceAccessException(this, e);
+ } finally {
+ closeStatement(statement);
+ closeStatement(statement2);
}
return revisionDescriptors;
}
@@ -1185,8 +1254,10 @@
// TODO : Here, we have the option of "cleaning up" before
// creating the new records in the database.
+ Statement statement = null;
+
try {
- Statement statement = connection.createStatement();
+ statement = connection.createStatement();
String s = null;
ResultSet res = null;
@@ -1221,6 +1292,8 @@
} catch (SQLException e) {
throw new ServiceAccessException(this, e);
+ } finally {
+ closeStatement(statement);
}
}
@@ -1254,11 +1327,12 @@
public void removeRevisionDescriptors(Uri uri)
throws ServiceAccessException {
+ Statement statement = null;
+
try {
- Statement statement = connection.createStatement();
+ statement = connection.createStatement();
String s = null;
- ResultSet res = null;
s = "delete from revisions where uri='" + uri.toString() + "'";
statement.execute(s);
@@ -1276,6 +1350,8 @@
} catch (SQLException e) {
throw new ServiceAccessException(this, e);
+ } finally {
+ closeStatement(statement);
}
}
@@ -1292,10 +1368,11 @@
throws ServiceAccessException, RevisionDescriptorNotFoundException {
NodeRevisionDescriptor revisionDescriptor = null;
+ Statement statement = null;
try {
- Statement statement = connection.createStatement();
+ statement = connection.createStatement();
String s = null;
ResultSet res = null;
@@ -1351,8 +1428,12 @@
new NodeRevisionDescriptor(revisionNumber, branchName,
labels, properties);
+ res.close();
+
} catch (SQLException e) {
throw new ServiceAccessException(this, e);
+ } finally {
+ closeStatement(statement);
}
return revisionDescriptor;
@@ -1370,9 +1451,11 @@
(Uri uri, NodeRevisionDescriptor revisionDescriptor)
throws ServiceAccessException {
+ Statement statement = null;
+
try {
- Statement statement = connection.createStatement();
+ statement = connection.createStatement();
String s = null;
ResultSet res = null;
@@ -1412,6 +1495,8 @@
} catch (SQLException e) {
throw new ServiceAccessException(this, e);
+ } finally {
+ closeStatement(statement);
}
}
@@ -1446,11 +1531,12 @@
public void removeRevisionDescriptor(Uri uri, NodeRevisionNumber number)
throws ServiceAccessException {
+ Statement statement = null;
+
try {
- Statement statement = connection.createStatement();
+ statement = connection.createStatement();
String s = null;
- ResultSet res = null;
s = "delete from revision where uri='" + uri + "' and xnumber='"
+ number + "'";
@@ -1470,8 +1556,27 @@
} catch (SQLException e) {
throw new ServiceAccessException(this, e);
+ } finally {
+ closeStatement(statement);
}
}
+
+
+ // ------------------------------------------------------ Protected Methods
+
+
+ /**
+ * Close specified statement.
+ */
+ protected void closeStatement(Statement statement) {
+ if (statement != null) {
+ try {
+ statement.close();
+ } catch (SQLException e) {
+ }
+ }
+ }
+
}