newbie
|
Posted: 09/19/2005, 8:31 AM |
|
If I use "Before Insert" event and insert in the first table, and then insert to the 2nd table fails, I want to rollback the changes in the first table and remove the just inserted record.
How do I do this?
Also, can I just get the Connection that CCS uses, instead of degining a new conn object?
I am using the following code from CCS samples:
JDBCConnection conn = JDBCConnectionFactory.getJDBCConnection("ashishocg");
String sql = "INSERT INTO report (a) VALUES ('aaa')";
conn.executeUpdate(sql);
conn.closeConnection();
|
|
|
Richard Brown
|
Posted: 10/21/2005, 1:01 AM |
|
I use this code below to do this:-
DBConnectionManager DBM;
Connection cn = null;
Statement stmt = null;
ResultSet rs = null;
int pk = 0;
Integer num = 0;
try {
DBM = DBConnectionManager.getInstance();
cn = DBM.getConnection("connection1");
cn.setAutoCommit(false);
stmt = cn.createStatement();
stmt.execute("update nextkey set id = id+1 where tablename='rep_composition'");
rs = stmt.executeQuery("select id from tablename where tablename='rep_composition'");
// Get new ID.
pk = Utils.convertToLong(rs.getInt("id")).intValue();
num = new Integer(pk);
// commit trans.
cn.commit();
} catch (Exception ex) {
try {
if (cn != null) {
cn.rollback();
}
e.getRecord().addError("Database error: " + ex.getMessage());
} catch (Exception ex2) {
e.getRecord().addError("Database error: " + ex2.getMessage());
}
} finally {
try {
if (rs != null) {
rs = null;
}
if (stmt != null) {
stmt = null;
}
if (cn != null) {
if (cn.isClosed() == false) {
cn.close();
}
cn = null;
}
} catch (Exception ex) {
e.getRecord().addError("Database error: " + ex.getMessage());
}
}
|
|
|
|