Skip navigation links

MySQL Forums :: Connector/JDBC and Java :: JDBC and Rollbacks with Savepoints


Advanced Search

JDBC and Rollbacks with Savepoints
Posted by: Carsten Bühler ()
Date: September 05, 2012 04:50AM

Hello,

I got a problem rollback changes over my JDBC-connection. I am not using any super-user and the Tables are of engine=innoDB.

Problem ist, that after a failure in executeBatch no rollbakc can be done, because the savepoint cant be found.


### I forgot: Many thanks in advance.

Here are some fragments of my code:

Connection conn = DatabaseConnection.getConnection();
Statement stmt = null;

Savepoint sp = null;

try {

conn.setAutoCommit(false);

try {
sp = conn.setSavepoint("sp1");
} catch (SQLException e) {
log.error("Error creating savepoint: ", e);
}

[...]

while ((str = in.readLine()) != null) {

if (log.isDebugEnabled())
log.debug("#SQL-Statement: " + str);

stmt.addBatch(str);
}

in.close();

result = stmt.executeBatch();

conn.commit();

} catch (BatchUpdateException e) {
try {
log.error("Error in SQL-Script: ",e);
if (sp!=null) {
log.debug("Doing rollback with SavePoint.");
conn.rollback(sp);
}
else {
log.debug("Doing normal rollback without SavePoint.");
conn.rollback();
}
conn.commit();
return false;
} catch (Exception te) {
log.debug("Rollback konnte nicht durchgeführt werden! ", te);
}
}
catch (Exception ex) {
log.error("Script: " + filePath + " was not able to be commited to database!");
log.error("ExecuteDatabaseScript: ", ex);
return false;
} finally {
try {
if (sp!=null && conn!=null) conn.releaseSavepoint(sp);
} catch (SQLException e) {
log.error("Error releasing savepoint: ", e);
}
DatabaseConnection.closeStatement(stmt);
DatabaseConnection.closeConnection(conn);
}

Options: ReplyQuote


Subject Written By Posted
JDBC and Rollbacks with Savepoints Carsten Bühler 09/05/2012 04:50AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.