1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.transaction.managed;
17
18 import java.sql.Connection;
19 import java.sql.SQLException;
20
21 import javax.sql.DataSource;
22
23 import org.apache.ibatis.logging.Log;
24 import org.apache.ibatis.logging.LogFactory;
25 import org.apache.ibatis.session.TransactionIsolationLevel;
26 import org.apache.ibatis.transaction.Transaction;
27
28
29
30
31
32
33
34
35
36
37
38
39 public class ManagedTransaction implements Transaction {
40
41 private static final Log log = LogFactory.getLog(ManagedTransaction.class);
42
43 private DataSource dataSource;
44 private TransactionIsolationLevel level;
45 private Connection connection;
46 private boolean closeConnection;
47
48 public ManagedTransaction(Connection connection, boolean closeConnection) {
49 this.connection = connection;
50 this.closeConnection = closeConnection;
51 }
52
53 public ManagedTransaction(DataSource ds, TransactionIsolationLevel level, boolean closeConnection) {
54 this.dataSource = ds;
55 this.level = level;
56 this.closeConnection = closeConnection;
57 }
58
59 @Override
60 public Connection getConnection() throws SQLException {
61 if (this.connection == null) {
62 openConnection();
63 }
64 return this.connection;
65 }
66
67 @Override
68 public void commit() throws SQLException {
69
70 }
71
72 @Override
73 public void rollback() throws SQLException {
74
75 }
76
77 @Override
78 public void close() throws SQLException {
79 if (this.closeConnection && this.connection != null) {
80 if (log.isDebugEnabled()) {
81 log.debug("Closing JDBC Connection [" + this.connection + "]");
82 }
83 this.connection.close();
84 }
85 }
86
87 protected void openConnection() throws SQLException {
88 if (log.isDebugEnabled()) {
89 log.debug("Opening JDBC Connection");
90 }
91 this.connection = this.dataSource.getConnection();
92 if (this.level != null) {
93 this.connection.setTransactionIsolation(this.level.getLevel());
94 }
95 }
96
97 }