From 0a38c002d63c43b1d49d2276bb38c155ba1d9782 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Wed, 5 Oct 2016 02:23:31 +0200 Subject: [PATCH] Closing data base connection (it is required even when pooled connection) --- .../database/impl/DataBaseAccessImpl.java | 63 +++++++++++----------- .../datasource/impl/DoDataSourceContext.java | 18 ++++--- 2 files changed, 42 insertions(+), 39 deletions(-) diff --git a/src/main/java/com/prueba/core/context/integration/database/impl/DataBaseAccessImpl.java b/src/main/java/com/prueba/core/context/integration/database/impl/DataBaseAccessImpl.java index 54564df..debe67a 100644 --- a/src/main/java/com/prueba/core/context/integration/database/impl/DataBaseAccessImpl.java +++ b/src/main/java/com/prueba/core/context/integration/database/impl/DataBaseAccessImpl.java @@ -41,23 +41,22 @@ public class DataBaseAccessImpl implements DataBaseAccess { protected List> executeQueryThrowable( String query, ExecuteResultSet executeResultSet, FillPreparedStatement fillStatement) throws SQLException { - try { - final Connection connection = this.dataSource.getConnection(); - - return this.doExecuteQuery( - query, - connection, - preparedStatement -> - { - fillStatement.doFill(preparedStatement); - - return preparedStatement.executeQuery(); - }, - executeResultSet - ); - } finally { - dataSource.getConnection().close(); - } + + final Connection connection = this.dataSource.getConnection(); + try { + return this.doExecuteQuery( + query, + connection, + preparedStatement -> + { + fillStatement.doFill(preparedStatement); + + return preparedStatement.executeQuery(); + }, + executeResultSet); + } finally { + connection.close(); + } } protected List> doExecuteQuery ( @@ -88,21 +87,21 @@ public class DataBaseAccessImpl implements DataBaseAccess { protected void executeUpdateThrowable( String query, FillPreparedStatement fillStatement) throws SQLException { - try { - final Connection connection = this.dataSource.getConnection(); - - this.doExecuteUpdate( - query, - connection, - preparedStatement -> - { - fillStatement.doFill(preparedStatement); - - preparedStatement.executeUpdate(); - }); - } finally { - dataSource.getConnection().close(); - } + + final Connection connection = this.dataSource.getConnection(); + try { + this.doExecuteUpdate( + query, + connection, + preparedStatement -> + { + fillStatement.doFill(preparedStatement); + + preparedStatement.executeUpdate(); + }); + } finally { + connection.close(); + } } protected void doExecuteUpdate ( diff --git a/src/main/java/com/prueba/core/context/integration/datasource/impl/DoDataSourceContext.java b/src/main/java/com/prueba/core/context/integration/datasource/impl/DoDataSourceContext.java index f5b6025..18a352a 100644 --- a/src/main/java/com/prueba/core/context/integration/datasource/impl/DoDataSourceContext.java +++ b/src/main/java/com/prueba/core/context/integration/datasource/impl/DoDataSourceContext.java @@ -17,14 +17,14 @@ public class DoDataSourceContext implements DoDataSource { private static final String USERNAME = "sa"; private static final String PASSWORD = ""; - private final ComboPooledDataSource pool; + private final DataSource dataSource; - private DoDataSourceContext(ComboPooledDataSource pool) { - this.pool = pool; + private DoDataSourceContext() { + this.dataSource = doDataSource(); } private static class DoDataSourceContextHolder { - private static final DoDataSourceContext INSTANCE = new DoDataSourceContext(new ComboPooledDataSource()); + private static final DoDataSourceContext INSTANCE = new DoDataSourceContext(); } public static DoDataSource getInstance() { @@ -33,7 +33,10 @@ public class DoDataSourceContext implements DoDataSource { @Override public DataSource getDataSource() { - + return this.dataSource; + } + + protected DataSource doDataSource() { DataSource dataSource = null; try { dataSource = doDataSourceThrowable(); @@ -44,9 +47,10 @@ public class DoDataSourceContext implements DoDataSource { } return dataSource; - } - + } + private DataSource doDataSourceThrowable() throws PropertyVetoException { + final ComboPooledDataSource pool = new ComboPooledDataSource(); pool.setUser(USERNAME); pool.setPassword(PASSWORD); pool.setDriverClass(DRIVER_CLASS); -- 2.1.4