|
From: Stefan K. <ste...@co...> - 2003-07-28 20:56:08
|
Hello,
We are using the SQL journal in a multi-threaded J2EE environment and got
some DB error, because of duplicated IDs in LOG table.
We solved it by making getNextStep and the DB Update a critical section. I
think, this makes sense and should go into the babeldoc code.
Regards,
Stefan
// copied from GenericSQLJournal with synchronization
// getNextStep + DB Update is a critical section
protected void log(IJournalTicket ticket, JournalOperation operation,
String other, String stage, Object data) throws
JournalException {
//getLog().debug("[GenericSqlJournal.log] called");
// Get the next step for the ticket
if (!ticket.isDummy()) {
PreparedStatement pstmt = null;
Connection con = null;
String logInsertStmt = SqlQueryManager.getSqlQuery(LOG_INSERT);
//getLog().debug("[GenericSqlJournal.log] sql: "+logInsertStmt);
long id = ((JournalTicket)ticket).getValue();
try {
con = (Connection) resource.checkOut();
pstmt = con.prepareStatement(logInsertStmt);
String pstageName = (stage != null) ? stage : "null";
String additional = "";
if (operation.equals(JournalOperation.updateStatus)) {
additional = (String) data;
}
//getLog().debug("[GenericSqlJournal.log] Writing step: "+step);
pstmt.setLong(1, id);
pstmt.setLong(3, new java.util.Date().getTime());
pstmt.setString(4, operation.toString());
pstmt.setString(5, other);
pstmt.setString(6, pstageName);
pstmt.setString(7, additional);
long step ;
// without synchronized it is likely to get the same value
// from getNextStep several times in multiple threads
synchronized ( this.getClass() ) {
step = getNextStep(con, id);
pstmt.setLong(2, step);
pstmt.executeUpdate();
}
if (operation.equals(JournalOperation.updateDocument)) {
writeDelta(con, id, step,
(com.babeldoc.core.pipeline.PipelineDocument) data);
}
} catch (Exception se) {
LogService.getInstance().logError(se);
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
resource.checkIn(con);
} catch (Exception se) {
LogService.getInstance().logError(I18n.get("sql.403"), se);
}
}
}
}
|