Closing data base connection (it is required even when pooled connection)
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Wed, 5 Oct 2016 00:23:31 +0000 (02:23 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Wed, 5 Oct 2016 00:23:31 +0000 (02:23 +0200)
src/main/java/com/prueba/core/context/integration/database/impl/DataBaseAccessImpl.java
src/main/java/com/prueba/core/context/integration/datasource/impl/DoDataSourceContext.java

index 54564df..debe67a 100644 (file)
@@ -41,23 +41,22 @@ public class DataBaseAccessImpl implements DataBaseAccess {
        
        protected List<Map<String, String>> executeQueryThrowable(
                        String query, ExecuteResultSet<ResultSet> 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<Map<String, String>> 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 (
index f5b6025..18a352a 100644 (file)
@@ -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);