01: package com.xoetrope.service.replication.hsqldb;
02:
03: import java.sql.Statement;
04: import net.xoetrope.optional.data.sql.ConnectionObject;
05: import net.xoetrope.optional.data.sql.NamedConnectionManager;
06: import org.hsqldb.Trigger;
07:
08: public class XRemoteInsertTrigger implements Trigger {
09: private static String CONNECTION_NAME = "default";
10: protected NamedConnectionManager connMgr;
11:
12: public XRemoteInsertTrigger() {
13: connMgr = (NamedConnectionManager) NamedConnectionManager
14: .getInstance();
15: }
16:
17: public void fire(int type, String trigName, String tableName,
18: Object[] oldRow, Object[] newRow) {
19: ConnectionObject connObj = null;
20:
21: try {
22: connObj = connMgr.getConnection(CONNECTION_NAME);
23: } catch (Exception ex) {
24: ex.printStackTrace();
25: throw new RuntimeException(
26: "Cannot establish a new connection.");
27: }
28:
29: try {
30: String sql = ("UPDATE XSYSSERVERTIMESTAMPS SET lastUpdate=CURRENT_TIMESTAMP "
31: + "WHERE tableName='" + tableName + "'");
32:
33: Statement stmt = connObj.createStatement();
34: stmt.executeUpdate(sql);
35: } catch (Exception ex) {
36: ex.printStackTrace();
37: throw new RuntimeException(
38: "Error while updating the XSYSSERVERTIMESTAMPS table.");
39: } finally {
40: if (connObj != null)
41: connObj.closeStatement();
42: }
43:
44: try {
45: if (newRow.length < 3)
46: return;
47: Object pseudoId = newRow[newRow.length - 2];
48: String sql = ("UPDATE " + tableName + " SET " + tableName
49: + "_ts=CURRENT_TIMESTAMP " + "WHERE PseudoId=" + String
50: .valueOf(pseudoId));
51:
52: Statement stmt = connObj.createStatement();
53: stmt.executeUpdate(sql);
54: } catch (Exception ex) {
55: ex.printStackTrace();
56: throw new RuntimeException("Error while updating the "
57: + tableName + " table");
58: } finally {
59: if (connObj != null) {
60: connObj.closeStatement();
61: }
62: }
63:
64: }
65:
66: }
|