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 public class ManagedTransaction implements Transaction {
39
40 private static final Log log = LogFactory.getLog(ManagedTransaction.class);
41
42 private DataSource dataSource;
43 private TransactionIsolationLevel level;
44 private Connection connection;
45 private final boolean closeConnection;
46
47 public ManagedTransaction(Connection connection, boolean closeConnection) {
48 this.connection = connection;
49 this.closeConnection = closeConnection;
50 }
51
52 public ManagedTransaction(DataSource ds, TransactionIsolationLevel level, boolean closeConnection) {
53 this.dataSource = ds;
54 this.level = level;
55 this.closeConnection = closeConnection;
56 }
57
58 @Override
59 public Connection getConnection() throws SQLException {
60 if (this.connection == null) {
61 openConnection();
62 }
63 return this.connection;
64 }
65
66 @Override
67 public void commit() throws SQLException {
68
69 }
70
71 @Override
72 public void rollback() throws SQLException {
73
74 }
75
76 @Override
77 public void close() throws SQLException {
78 if (this.closeConnection && this.connection != null) {
79 if (log.isDebugEnabled()) {
80 log.debug("Closing JDBC Connection [" + this.connection + "]");
81 }
82 this.connection.close();
83 }
84 }
85
86 protected void openConnection() throws SQLException {
87 if (log.isDebugEnabled()) {
88 log.debug("Opening JDBC Connection");
89 }
90 this.connection = this.dataSource.getConnection();
91 if (this.level != null) {
92 this.connection.setTransactionIsolation(this.level.getLevel());
93 }
94 }
95
96 @Override
97 public Integer getTimeout() throws SQLException {
98 return null;
99 }
100
101 }