001: /*
002: * Copyright 2006 Davide Deidda
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: /*
018: * Transaction.java
019: *
020: * Created on 10 aprile 2005, 8.50
021: */
022:
023: package it.biobytes.ammentos;
024:
025: import java.sql.*;
026: import java.util.*;
027: import static java.sql.Connection.*;
028:
029: /**
030: *
031: * @author davide
032: */
033: public class Transaction {
034: private TransactionConnection m_connection;
035: private List<Object> m_savedObjects;
036: private List<Object> m_deletedObjects;
037: private int m_isolationLevel; // Tr. isolation level
038: private PersistenceContext m_context; // Context
039:
040: /** Represents a transaction into the framework */
041: public Transaction(int isolationLevel, PersistenceContext context) {
042: m_context = context;
043: m_savedObjects = new ArrayList<Object>();
044: m_deletedObjects = new ArrayList<Object>();
045: switch (isolationLevel) {
046: case TRANSACTION_NONE:
047: case TRANSACTION_SERIALIZABLE:
048: case TRANSACTION_READ_COMMITTED:
049: case TRANSACTION_REPEATABLE_READ:
050: case TRANSACTION_READ_UNCOMMITTED:
051: m_isolationLevel = isolationLevel;
052: break;
053: default:
054: m_isolationLevel = TRANSACTION_READ_UNCOMMITTED;
055: break;
056: }
057: }
058:
059: public void commit() throws PersistenceException {
060: try {
061: m_connection.commit();
062: m_connection.setAutoCommit(true);
063: } catch (SQLException e) {
064: throw new PersistenceException(e);
065: } finally {
066: try {
067: m_connection.transactionClose();
068: } catch (Exception e) {
069: }
070: }
071: }
072:
073: public void rollback() throws PersistenceException {
074: try {
075: m_connection.rollback();
076: m_connection.setAutoCommit(true);
077: } catch (SQLException e) {
078: throw new PersistenceException(e);
079: } finally {
080: try {
081: m_connection.transactionClose();
082: } catch (Exception e) {
083: }
084: }
085: }
086:
087: /**
088: * Returns a Database wrapped into a TransactionConnection, which is managed
089: * slightly differently (close() is ignored, transactionClose() is used instead)
090: */
091: public Connection getDbConnection() throws PersistenceException {
092: try {
093: if (m_connection == null) {
094: m_connection = new TransactionConnection(m_context
095: .getDataSource().getConnection());
096: m_connection.setAutoCommit(false);
097: m_connection.setTransactionIsolation(m_isolationLevel);
098: }
099: } catch (SQLException e) {
100: throw new PersistenceException(e);
101: }
102: return m_connection;
103: }
104:
105: void involveSavedObject(Object obj) {
106: m_savedObjects.add(obj);
107: }
108:
109: void involveDeletedObject(Object obj) {
110: m_deletedObjects.add(obj);
111: }
112:
113: List<Object> savedObjects() {
114: return Collections.unmodifiableList(m_savedObjects);
115: }
116:
117: List<Object> deletedObjects() {
118: return Collections.unmodifiableList(m_deletedObjects);
119: }
120: }
|