1 | class ManagedProviderConnectionHelper implements ConnectionHelper {↵ | | 1 | class SuppliedConnectionProviderConnectionHelper implements ConnectionHelper {↵
|
2 | private Properties cfgProperties;↵ | | 2 | private ConnectionProvider provider;↵
|
3 | private ConnectionProvider connectionProvider;↵ | | 3 | private Connection connection;↵
|
4 | private Connection connection;↵ | | 4 | private ↵
|
|
5 | public ManagedProviderConnectionHelper(Properties cfgProperties) {↵ | | |
|
6 | this.cfgProperties = cfgProperties↵ | | 5 | boolean toggleAutoCommit;↵
|
|
| | | 6 | public SuppliedConnectionProviderConnectionHelper(ConnectionProvider provider) {↵
|
7 | ;↵ | | 7 | this.provider = provider;↵
|
8 | }↵ | | 8 | }↵
|
|
9 | public void prepare(boolean needsAutoCommit) throws SQLException {↵ | | 9 | public void prepare(boolean needsAutoCommit) throws SQLException {↵
|
10 | connectionProvider = ConnectionProviderFactory.newConnectionProvider( cfgProperties );↵ | | 10 | connection↵
|
11 | connection = connectionProvider.getConnection();↵ | | |
|
12 | if ( needsAutoCommit && !connection.getAutoCommit() ) {↵ | | |
|
13 | connection.commit();↵ | | 11 | = provider.getConnection();↵
|
| | | 12 | toggleAutoCommit = needsAutoCommit && !connection.getAutoCommit();↵
|
| | | 13 | if ( toggleAutoCommit ) {↵
|
| | | 14 | try {↵
|
| | | 15 | connection.commit();↵
|
| | | 16 | }↵
|
| | | 17 | catch( Throwable ignore ) {↵
|
| | | 18 | // might happen with a managed connection↵
|
| | | 19 | }↵
|
14 | connection.setAutoCommit( true );↵ | | 20 | connection.setAutoCommit( true );↵
|
15 | }↵ | | 21 | }↵
|
16 | }↵ | | 22 | }↵
|
|
17 | public Connection getConnection() throws SQLException {↵ | | 23 | public Connection getConnection() throws SQLException {↵
|
18 | return connection;↵ | | 24 | return connection;↵
|
19 | }↵ | | 25 | }↵
|
|
20 | public void release() throws SQLException {↵ | | 26 | public void release() throws SQLException {↵
|
21 | ↵ | | 27 | // we only release the connection↵
|
22 | if ( connection != null ) {↵ | | 28 | if ( connection != null ) {↵
|
23 | try {↵ | | 29 | ↵
|
24 | JDBCExceptionReporter.logAndClearWarnings( connection );↵ | | 30 | JDBCExceptionReporter.logAndClearWarnings( connection );↵
|
25 | connectionProvider.closeConnection( connection );↵ | | 31 | ↵
|
26 | }↵ | | |
|
27 | finally {↵ | | |
|
28 | connectionProvider.close();↵ | | |
|
29 | }↵ | | |
|
30 | }↵ | | |
|
| | | 32 | if ( toggleAutoCommit ) {↵
|
| | | 33 | connection.setAutoCommit( false );↵
|
| | | 34 | }↵
|
| | | 35 | provider.closeConnection( connection );↵
|
31 | connection = null | | 36 | connection = null
|