|
From: <pe...@us...> - 2003-12-01 17:11:04
|
Update of /cvsroot/neuclear/neuclear-ledger/src/java/org/neuclear/ledger/implementations
In directory sc8-pr-cvs1:/tmp/cvs-serv9746/org/neuclear/ledger/implementations
Modified Files:
SQLLedger.java
Log Message:
Added initial Support for entityengine (OFBiz)
Index: SQLLedger.java
===================================================================
RCS file: /cvsroot/neuclear/neuclear-ledger/src/java/org/neuclear/ledger/implementations/SQLLedger.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** SQLLedger.java 21 Nov 2003 04:43:20 -0000 1.3
--- SQLLedger.java 1 Dec 2003 17:11:01 -0000 1.4
***************
*** 9,14 ****
--- 9,19 ----
import org.neuclear.commons.sql.ConnectionSource;
import org.neuclear.commons.sql.SQLTools;
+ import org.neuclear.commons.sql.SQLContext;
+ import org.neuclear.commons.NeuClearException;
import org.neuclear.ledger.*;
+ import org.neuclear.ledger.InvalidTransactionException;
+ import javax.transaction.*;
+ import javax.naming.NamingException;
import java.io.IOException;
import java.sql.*;
***************
*** 38,42 ****
public SQLLedger(final ConnectionSource con, final String id) throws LowlevelLedgerException, UnknownLedgerException {
super(id, getLedgerName(con, id));
! this.con = con;
}
--- 43,47 ----
public SQLLedger(final ConnectionSource con, final String id) throws LowlevelLedgerException, UnknownLedgerException {
super(id, getLedgerName(con, id));
! this.con = new SQLContext(con);
}
***************
*** 80,95 ****
if (bookExists(bookID))
throw new BookExistsException(this, bookID);
! try {
! getConnection().setAutoCommit(false);
final PreparedStatement stmt = prepQuery("insert into account values (?,?,3)");
stmt.setString(1, bookID);
stmt.setString(2, title);
stmt.execute();
! getConnection().commit();
} catch (SQLException e) {
throw new LowlevelLedgerException(this, e);
}
- return createBookInstance(bookID, title);
}
--- 85,99 ----
if (bookExists(bookID))
throw new BookExistsException(this, bookID);
! try {
final PreparedStatement stmt = prepQuery("insert into account values (?,?,3)");
stmt.setString(1, bookID);
stmt.setString(2, title);
stmt.execute();
! return createBookInstance(bookID, title);
} catch (SQLException e) {
+ rollbackUT();
throw new LowlevelLedgerException(this, e);
}
}
***************
*** 103,108 ****
}
try {
- getConnection().setAutoCommit(false);
-
final long xid = insertTransaction(transaction);
final Iterator items = transaction.getItems();
--- 107,110 ----
***************
*** 111,125 ****
insertTransactionItem(xid, item);
}
- getConnection().commit();
newid = Long.toString(xid);
} catch (SQLException e) {
! try {
! getConnection().rollback();
! } catch (SQLException e1) {
! throw new LowlevelLedgerException(this, e1);
! }
throw new LowlevelLedgerException(this, e);
}
-
return this.createTransaction(transaction, newid);
}
--- 113,121 ----
insertTransactionItem(xid, item);
}
newid = Long.toString(xid);
} catch (SQLException e) {
! rollbackUT();
throw new LowlevelLedgerException(this, e);
}
return this.createTransaction(transaction, newid);
}
***************
*** 139,144 ****
}
try {
- getConnection().setAutoCommit(false);
-
final long xid = insertHeldTransaction(transaction);
final Iterator items = transaction.getItems();
--- 135,138 ----
***************
*** 147,163 ****
insertHeldTransactionItem(xid, item);
}
- getConnection().commit();
newid = Long.toString(xid);
} catch (SQLException e) {
! try {
! getConnection().rollback();
! } catch (SQLException e1) {
! throw new LowlevelLedgerException(this, e1);
! }
System.err.println(e.getSQLState());
e.printStackTrace(System.err);
throw new LowlevelLedgerException(this, e);
}
-
return this.createHeldTransaction(transaction, newid);
}
--- 141,151 ----
insertHeldTransactionItem(xid, item);
}
newid = Long.toString(xid);
} catch (SQLException e) {
! rollbackUT();
System.err.println(e.getSQLState());
e.printStackTrace(System.err);
throw new LowlevelLedgerException(this, e);
}
return this.createHeldTransaction(transaction, newid);
}
***************
*** 176,187 ****
update.setString(2, getId());
final int affected = update.executeUpdate();
! if (affected == 0)
throw new UnknownTransactionException(this, hold.getXid());
if (affected > 1) {
! getConnection().rollback();
throw new LowlevelLedgerException(this, "performCancelHold: For some reason multiple rows were updated. Transaction Rolled Back.");
}
- getConnection().commit();
} catch (SQLException e) {
throw new LowlevelLedgerException(this, e);
}
--- 164,177 ----
update.setString(2, getId());
final int affected = update.executeUpdate();
! if (affected == 0) {
! rollbackUT();
throw new UnknownTransactionException(this, hold.getXid());
+ }
if (affected > 1) {
! rollbackUT();
throw new LowlevelLedgerException(this, "performCancelHold: For some reason multiple rows were updated. Transaction Rolled Back.");
}
} catch (SQLException e) {
+ rollbackUT();
throw new LowlevelLedgerException(this, e);
}
***************
*** 203,219 ****
update.setString(3, getId());
final int affected = update.executeUpdate();
! if (affected == 0)
throw new UnknownTransactionException(this, hold.getXid());
if (affected > 1) {
! getConnection().rollback();
throw new LowlevelLedgerException(this, "performCompleteHold: For some reason multiple rows were updated. Transaction Rolled Back.");
}
- getConnection().commit();
-
return tran;
} catch (SQLException e) {
throw new LowlevelLedgerException(this, e);
} catch (UnknownTransactionException e) {
throw new LowlevelLedgerException(this, e);
}
--- 193,211 ----
update.setString(3, getId());
final int affected = update.executeUpdate();
! if (affected == 0) {
! rollbackUT();
throw new UnknownTransactionException(this, hold.getXid());
+ }
if (affected > 1) {
! rollbackUT();
throw new LowlevelLedgerException(this, "performCompleteHold: For some reason multiple rows were updated. Transaction Rolled Back.");
}
return tran;
} catch (SQLException e) {
+ rollbackUT();
throw new LowlevelLedgerException(this, e);
} catch (UnknownTransactionException e) {
+ rollbackUT();
throw new LowlevelLedgerException(this, e);
}
***************
*** 222,228 ****
private long insertTransaction(final UnPostedTransaction transaction) throws SQLException, LowlevelLedgerException {
!
! final PreparedStatement tranInsert;
! tranInsert = prepQuery("insert into transaction (value_date,comment,ledgerid) values (?,?,?)");
tranInsert.setTimestamp(1, SQLTools.toTimestamp(transaction.getTransactionTime()));
tranInsert.setString(2, transaction.getComment());
--- 214,218 ----
private long insertTransaction(final UnPostedTransaction transaction) throws SQLException, LowlevelLedgerException {
! final PreparedStatement tranInsert = prepQuery("insert into transaction (value_date,comment,ledgerid) values (?,?,?)");
tranInsert.setTimestamp(1, SQLTools.toTimestamp(transaction.getTransactionTime()));
tranInsert.setString(2, transaction.getComment());
***************
*** 233,238 ****
if (rs.next())
return rs.getLong(1);
! else {
! getConnection().rollback();
throw new LowlevelLedgerException(this, "We couldnt get the id of the transaction. Safer to Rollback.");
}
--- 223,228 ----
if (rs.next())
return rs.getLong(1);
! else {
! rollbackUT();
throw new LowlevelLedgerException(this, "We couldnt get the id of the transaction. Safer to Rollback.");
}
***************
*** 240,246 ****
private long insertHeldTransaction(final UnPostedHeldTransaction transaction) throws SQLException, LowlevelLedgerException {
!
! final PreparedStatement tranInsert;
! tranInsert = prepQuery("insert into held_transaction (value_date,comment,held_until,ledgerid) values (?,?,?,?)");
tranInsert.setTimestamp(3, SQLTools.toTimestamp(transaction.getExpiryTime()));
--- 230,234 ----
private long insertHeldTransaction(final UnPostedHeldTransaction transaction) throws SQLException, LowlevelLedgerException {
! final PreparedStatement tranInsert = prepQuery("insert into held_transaction (value_date,comment,held_until,ledgerid) values (?,?,?,?)");
tranInsert.setTimestamp(3, SQLTools.toTimestamp(transaction.getExpiryTime()));
***************
*** 253,258 ****
if (rs.next())
return rs.getLong(1);
! else {
! getConnection().rollback();
throw new LowlevelLedgerException(this, "We couldnt get the id of the transaction. Safer to Rollback.");
}
--- 241,246 ----
if (rs.next())
return rs.getLong(1);
! else {
! rollbackUT();
throw new LowlevelLedgerException(this, "We couldnt get the id of the transaction. Safer to Rollback.");
}
***************
*** 426,444 ****
}
- /* (non-Javadoc)
- * @see org.neuclear.ledger.Ledger#beginLinkedTransaction()
- */
- public final void beginLinkedTransaction() {
- // TODO Auto-generated method stub
-
- }
-
- /* (non-Javadoc)
- * @see org.neuclear.ledger.Ledger#endLinkedTransactions()
- */
- public final void endLinkedTransactions() {
- // TODO Auto-generated method stub
-
- }
public final String toString() {
--- 414,417 ----
***************
*** 456,460 ****
}
- private final ConnectionSource con;
public final Book getBook(final String bookID) throws UnknownBookException, LowlevelLedgerException {
--- 429,432 ----
***************
*** 470,472 ****
--- 442,460 ----
throw new UnknownBookException(this, bookID);
}
+
+ /**
+ * Rolls back a JTA UserTransaction on the ledger. Not to be confused with a Ledger Transaction.
+ * @param ut
+ * @throws LowlevelLedgerException
+ */
+ public void rollbackUT(UserTransaction ut) throws LowlevelLedgerException {
+ super.rollbackUT(ut);
+ try {
+ con.close();
+ } catch (SQLException e) {
+ throw new LowlevelLedgerException(this,e);
+ }
+ }
+
+ private final SQLContext con;
}
|