RimuDB Logo
Database Access Framework for Java

Transactions

Transaction support is provided with the Session class. The following is an example of inserting a record within a transaction.

Session instances are created from a Database. The Session has methods commit(), close() and rollback().

Sessions hold a connection open until the close() method is called. Your application should always make sure it calls close().

The following example inserts two records using a transactional connection.


CompoundDatabase cdb;
Database db = cdb.getDatabase("production-db");

Session session = null;
try {

   // Get a transactional connection
   session = db.createSession();

   // Insert a record
   OrderTransactionDO orderTransaction1 = new OrderTransactionDO(cdb);
   orderTransaction1.setName(name);
   orderTransaction1.setValue(value);
   orderTransaction1.commit(session);

   // Insert another record
   OrderTransactionDO orderTransaction2 = new OrderTransactionDO(cdb);
   orderTransaction2.setName(name);
   orderTransaction2.setValue(value);
   orderTransaction2.commit(session);

   // Commit the transaction
   session.commit();

} finally {
   // Close the connection
   if (session != null) {
      session.close();
   }
}


The following example demonstrates how a unit of work is inserted into the database or rolled back if an error occurs. In this case all the records related to a single customer order are inserted using a session and then committed to the database. If an exception occurs they are all rolled back.


CompoundDatabase cdb;
Database db = cdb.getDatabase("production-db");

Session session = null;
try {

   // Get a transactional connection
   session = db.createSession();

   // Insert an order header record
   OrderHeaderDO orderHeader = new OrderHeaderDO(cdb);
   orderHeader.setOrderNr(8000);
   orderHeader.setCustomerID(20335);
   orderHeader.commit(session);

   // Insert an order address record
   OrderAddressDO orderAddress = new OrderAddressDO(cdb);
   orderAddress.setOrderNr(8000);
   orderAddress.setSequenceNr(1);
   orderAddress.setName("John Smith");
   orderAddress.setAddress1("10 Main St");
   orderAddress.setCity("New York");
   orderAddress.setState("NY");
   orderAddress.commit(session);

   // Insert order line records
   OrderLineDO orderLine1 = new OrderLineDO(cdb);
   orderLine1.setOrderNr(8000);
   orderLine1.setSequenceNr(1);
   orderLine1.setItemNumber("UB0001");
   orderLine1.setItemColor("BLUE");
   orderLine1.setQuantity(1);
   orderLine1.setPrice(new BigDecimal(12.99));
   orderLine1.commit(session);
 
   OrderLineDO orderLine2 = new OrderLineDO(cdb);
   orderLine2.setOrderNr(8000);
   orderLine2.setSequenceNr(2);
   orderLine2.setItemNumber("UB0020");
   orderLine2.setItemColor("YELLOW");
   orderLine2.setQuantity(1);
   orderLine2.setPrice(new BigDecimal(19.99));
   orderLine2.commit(session);
 
   OrderLineDO orderLine3 = new OrderLineDO(cdb);
   orderLine3.setOrderNr(8000);
   orderLine3.setSequenceNr(3);
   orderLine3.setItemNumber("UB0020");
   orderLine3.setItemColor("RED");
   orderLine3.setQuantity(1);
   orderLine3.setPrice(new BigDecimal(9.99));
   orderLine3.commit(session);
   
   // Commit the transaction
   session.commit();

} catch (Exception e) {

   // Don't save any records if we get an exception
   session.rollback();
   System.err.println("Unsuccessful");

} finally {
   // Close the connection
   if (session != null) {
      session.close();
   }
}