View Javadoc

1   package com.explosion.expfmodules.rdbmsconn.dbom.utils;
2   
3   /*
4    * =============================================================================
5    * 
6    * Copyright 2004 Stephen Cowx
7    * 
8    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9    * use this file except in compliance with the License. You may obtain a copy of
10   * the License at
11   * 
12   * http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
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