diff --git a/CHANGELOG.md b/CHANGELOG.md index 66f4f3fd..6f801544 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed + +- Added method ConfigRuntimeException.convertToRuntimeEx() +- Added CloseableDAOContextAbstract.newCloseableDAOContextDS() +- Added CloseableDAOContextAbstract.newCloseableDAOContextCF() + ## [8.6.5] - 2024-09-07 ### Added diff --git a/fj-core/src/main/java/org/fugerit/java/core/db/daogen/CloseableDAOContextAbstract.java b/fj-core/src/main/java/org/fugerit/java/core/db/daogen/CloseableDAOContextAbstract.java index c32e3eee..1d0886e7 100644 --- a/fj-core/src/main/java/org/fugerit/java/core/db/daogen/CloseableDAOContextAbstract.java +++ b/fj-core/src/main/java/org/fugerit/java/core/db/daogen/CloseableDAOContextAbstract.java @@ -1,7 +1,15 @@ package org.fugerit.java.core.db.daogen; +import lombok.extern.slf4j.Slf4j; +import org.fugerit.java.core.db.connect.ConnectionFactory; +import org.fugerit.java.core.db.connect.ConnectionFactoryCloseable; +import org.fugerit.java.core.db.dao.DAOException; + +import javax.sql.DataSource; +import java.sql.Connection; import java.util.HashMap; +@Slf4j public abstract class CloseableDAOContextAbstract implements CloseableDAOContext { private HashMap attributes; @@ -21,4 +29,30 @@ public void setAttribute(String key, Object value) { this.attributes.put( key , value ); } + public static CloseableDAOContext newCloseableDAOContextCF(ConnectionFactory cf) { + return new CloseableDAOContextAbstract() { + @Override + public Connection getConnection() throws DAOException { + return cf.getConnection(); + } + @Override + public void close() throws Exception { + cf.release(); + } + }; + } + + public static CloseableDAOContext newCloseableDAOContextDS(DataSource ds) { + return new CloseableDAOContextAbstract() { + @Override + public Connection getConnection() throws DAOException { + return DAOException.get( () -> ds.getConnection() ); + } + @Override + public void close() throws Exception { + log.debug( "close() doing nothing on datasource : {}", ds ); + } + }; + } + } diff --git a/fj-core/src/main/java/org/fugerit/java/core/db/daogen/DAOContext.java b/fj-core/src/main/java/org/fugerit/java/core/db/daogen/DAOContext.java index 12d57e50..46b9c1b4 100644 --- a/fj-core/src/main/java/org/fugerit/java/core/db/daogen/DAOContext.java +++ b/fj-core/src/main/java/org/fugerit/java/core/db/daogen/DAOContext.java @@ -7,6 +7,6 @@ public interface DAOContext extends AttributesHolder { - public Connection getConnection() throws DAOException; + Connection getConnection() throws DAOException; } diff --git a/fj-core/src/test/java/test/org/fugerit/java/core/db/connect/TestConnectionFactory.java b/fj-core/src/test/java/test/org/fugerit/java/core/db/connect/TestConnectionFactory.java index 8148f156..97ccf35c 100644 --- a/fj-core/src/test/java/test/org/fugerit/java/core/db/connect/TestConnectionFactory.java +++ b/fj-core/src/test/java/test/org/fugerit/java/core/db/connect/TestConnectionFactory.java @@ -18,6 +18,9 @@ import org.fugerit.java.core.db.connect.DbcpConnectionFactory; import org.fugerit.java.core.db.connect.SingleConnectionFactory; import org.fugerit.java.core.db.dao.DAOException; +import org.fugerit.java.core.db.daogen.CloseableDAOContext; +import org.fugerit.java.core.db.daogen.CloseableDAOContextAbstract; +import org.fugerit.java.core.function.SafeFunction; import org.fugerit.java.core.lang.helpers.ClassHelper; import org.fugerit.java.core.util.PropsIO; import org.fugerit.java.core.xml.dom.DOMIO; @@ -171,5 +174,27 @@ public void testCFDirect3() throws Exception { boolean ok = this.worker( ConnectionFactoryImpl.newInstance( new org.hsqldb.jdbcDriver(), "jdbc:hsqldb:mem:base_db_direct3", "testuser", "testp" ) ); Assert.assertTrue(ok); } + + @Test + public void testDaoContextOnDS() { + Assert.assertTrue( SafeFunction.get( () -> { + try (CloseableDAOContext context = CloseableDAOContextAbstract.newCloseableDAOContextDS(this.createDS()); + Connection conn = context.getConnection()) { + return Boolean.TRUE; + } + })); + } + + @Test + public void testDaoContextOnCF() { + Assert.assertTrue( SafeFunction.get( () -> { + try (CloseableDAOContext context = CloseableDAOContextAbstract.newCloseableDAOContextCF( + ConnectionFactoryImpl.newInstance( new org.hsqldb.jdbcDriver(), "jdbc:hsqldb:mem:base_db_daocontext1", "testuser", "testp" ) + ); + Connection conn = context.getConnection()) { + return Boolean.TRUE; + } + })); + } }