public void test_transactional_executeSQL_inputs() {
String countSql = "SELECT count(*) FROM pets";
// String tType = (String)CurrentThreadCache.get("key.TransactionStarterType");
// Object tr = CurrentThreadCache.get("key.Transactions");
TransactionManager tm = TransactionManagerUtil.getTransactionManager();
try{
tm.beginTransaction();
Object countBeforeInsert = SqlServiceClient.retrieveObjectBySQL(countSql);
assertEquals("Total rows countBeforeInsert", "13", countBeforeInsert.toString());
Object nextID = getNextPetID();
String sql = "INSERT INTO pets (id, name, type_id, owner_id) VALUES (?id, ?name, 1, 10)";
Map<String, Object> inputs = new HashMap<String, Object>();
inputs.put("id", nextID);
inputs.put("name", "Pingping");
int insertCount = SqlServiceClient.executeSQL(sql, inputs);
assertEquals("number of rows inserted", 1, insertCount);
String sql2 = "SELECT name FROM pets WHERE name = 'Pingping'";
Object data = SqlServiceClient.retrieveObjectBySQL(sql2);
assertEquals("name of the new pet", "Pingping", data.toString());
Object countAfterInsert = SqlServiceClient.retrieveObjectBySQL(countSql);
assertEquals("Total rows countAfterInsert", "14", countAfterInsert.toString());
//artificially creating an exception
int i = 1;
int j = 0;
System.out.println("You should not see this line: " + i/j);
tm.commitTransaction();
}
catch (Exception ex) {
tm.rollbackTransaction();
Object countAfterRollback = SqlServiceClient.retrieveObjectBySQL(countSql);
assertEquals("Total rows countAfterRollback Pingping", "13", countAfterRollback.toString());
}
finally {
tm.releaseResources();
}
Object countTheEnd = SqlServiceClient.retrieveObjectBySQL(countSql);
assertEquals("Total rows countTheEnd", "13", countTheEnd.toString());
}