@@ -65,11 +65,12 @@ public Configuration getConfiguration() {
65
65
}
66
66
67
67
private SqlSession openSessionFromDataSource (ExecutorType execType , TransactionIsolationLevel level , boolean autoCommit ) {
68
+ Connection connection = null ;
68
69
try {
69
70
final Environment environment = configuration .getEnvironment ();
70
71
final DataSource dataSource = getDataSourceFromEnvironment (environment );
71
72
TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment (environment );
72
- Connection connection = dataSource .getConnection ();
73
+ connection = dataSource .getConnection ();
73
74
if (level != null ) {
74
75
connection .setTransactionIsolation (level .getLevel ());
75
76
}
@@ -78,6 +79,7 @@ private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionI
78
79
Executor executor = configuration .newExecutor (tx , execType );
79
80
return new DefaultSqlSession (configuration , executor , autoCommit );
80
81
} catch (SQLException e ) {
82
+ closeConnection (connection );
81
83
throw ExceptionFactory .wrapException ("Error opening session. Cause: " + e , e );
82
84
} finally {
83
85
ErrorContext .instance ().reset ();
@@ -129,5 +131,15 @@ private Connection wrapConnection(Connection connection) {
129
131
}
130
132
}
131
133
134
+ private void closeConnection (Connection connection ) {
135
+ if (connection != null ) {
136
+ try {
137
+ connection .close ();
138
+ } catch (SQLException e1 ) {
139
+ // Intentionally ignore. Prefer previous error.
140
+ }
141
+ }
142
+ }
143
+
132
144
}
133
145
0 commit comments