1 package com.explosion.expfmodules.rdbmsconn.dbom.utils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.sql.Connection;
23 import java.sql.SQLException;
24 import java.sql.Savepoint;
25
26 import org.apache.log4j.LogManager;
27 import org.apache.log4j.Logger;
28
29 import com.explosion.expfmodules.rdbmsconn.connect.ConnectionManager;
30 import com.explosion.expfmodules.rdbmsconn.dbom.dialect.Dialect;
31 import com.explosion.utilities.exception.ExceptionManagerFactory;
32
33 /***
34 * This class allows a programmer to handle a JDBC call wrapped in an exception
35 *
36 * @author Stephen Cowx
37 * Created on 05-Jul-2005
38 */
39 public class Transaction
40 {
41 private static Logger log = LogManager.getLogger(Transaction.class);
42 private Connection connection;
43 private Savepoint savePoint;
44 private boolean supportsRollBack = false;
45 private boolean supportsSavePoints = false;
46 /***
47 * Constructs a transaction object for the given connection
48 * @param conn
49 */
50 public Transaction(Connection connection)
51 {
52 Dialect dialect = ConnectionManager.getInstance().getDialect(connection);
53 supportsSavePoints = dialect.supportsSavePoints();
54 supportsRollBack = dialect.supportsCommitAndRollback();
55
56 this.connection = connection;
57 }
58
59 public void begin() throws SQLException
60 {
61 if (supportsSavePoints)
62 savePoint = connection.setSavepoint();
63 }
64
65 public void commit() throws SQLException
66 {
67 connection.commit();
68 }
69
70 public void rollback()
71 {
72 try {
73 if (supportsSavePoints)
74 connection.rollback(savePoint);
75 else if (supportsRollBack)
76 connection.rollback();
77
78 } catch (SQLException e) {
79 log.error("Exception caught while rolling back transaction");
80 ExceptionManagerFactory.getExceptionManager().manageException(e,"Exception caught while rolling back transaction.");
81 }
82 }
83 }
84
85