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