Skip to content

Commit 587a185

Browse files
committed
Added CloseableDAOContextAbstract.newCloseableDAOContextDS()
Added CloseableDAOContextAbstract.newCloseableDAOContextCF()
1 parent 3ebe8ac commit 587a185

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- Added method ConfigRuntimeException.convertToRuntimeEx()
13+
- Added CloseableDAOContextAbstract.newCloseableDAOContextDS()
14+
- Added CloseableDAOContextAbstract.newCloseableDAOContextCF()
15+
1016
## [8.6.5] - 2024-09-07
1117

1218
### Added

fj-core/src/main/java/org/fugerit/java/core/db/daogen/CloseableDAOContextAbstract.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
package org.fugerit.java.core.db.daogen;
22

3+
import lombok.extern.slf4j.Slf4j;
4+
import org.fugerit.java.core.db.connect.ConnectionFactory;
5+
import org.fugerit.java.core.db.connect.ConnectionFactoryCloseable;
6+
import org.fugerit.java.core.db.dao.DAOException;
7+
8+
import javax.sql.DataSource;
9+
import java.sql.Connection;
310
import java.util.HashMap;
411

12+
@Slf4j
513
public abstract class CloseableDAOContextAbstract implements CloseableDAOContext {
614

715
private HashMap<String, Object> attributes;
@@ -21,4 +29,30 @@ public void setAttribute(String key, Object value) {
2129
this.attributes.put( key , value );
2230
}
2331

32+
public static CloseableDAOContext newCloseableDAOContextCF(ConnectionFactory cf) {
33+
return new CloseableDAOContextAbstract() {
34+
@Override
35+
public Connection getConnection() throws DAOException {
36+
return cf.getConnection();
37+
}
38+
@Override
39+
public void close() throws Exception {
40+
cf.release();
41+
}
42+
};
43+
}
44+
45+
public static CloseableDAOContext newCloseableDAOContextDS(DataSource ds) {
46+
return new CloseableDAOContextAbstract() {
47+
@Override
48+
public Connection getConnection() throws DAOException {
49+
return DAOException.get( () -> ds.getConnection() );
50+
}
51+
@Override
52+
public void close() throws Exception {
53+
log.debug( "close() doing nothing on datasource : {}", ds );
54+
}
55+
};
56+
}
57+
2458
}

fj-core/src/main/java/org/fugerit/java/core/db/daogen/DAOContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77

88
public interface DAOContext extends AttributesHolder {
99

10-
public Connection getConnection() throws DAOException;
10+
Connection getConnection() throws DAOException;
1111

1212
}

fj-core/src/test/java/test/org/fugerit/java/core/db/connect/TestConnectionFactory.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
import org.fugerit.java.core.db.connect.DbcpConnectionFactory;
1919
import org.fugerit.java.core.db.connect.SingleConnectionFactory;
2020
import org.fugerit.java.core.db.dao.DAOException;
21+
import org.fugerit.java.core.db.daogen.CloseableDAOContext;
22+
import org.fugerit.java.core.db.daogen.CloseableDAOContextAbstract;
23+
import org.fugerit.java.core.function.SafeFunction;
2124
import org.fugerit.java.core.lang.helpers.ClassHelper;
2225
import org.fugerit.java.core.util.PropsIO;
2326
import org.fugerit.java.core.xml.dom.DOMIO;
@@ -171,5 +174,27 @@ public void testCFDirect3() throws Exception {
171174
boolean ok = this.worker( ConnectionFactoryImpl.newInstance( new org.hsqldb.jdbcDriver(), "jdbc:hsqldb:mem:base_db_direct3", "testuser", "testp" ) );
172175
Assert.assertTrue(ok);
173176
}
177+
178+
@Test
179+
public void testDaoContextOnDS() {
180+
Assert.assertTrue( SafeFunction.get( () -> {
181+
try (CloseableDAOContext context = CloseableDAOContextAbstract.newCloseableDAOContextDS(this.createDS());
182+
Connection conn = context.getConnection()) {
183+
return Boolean.TRUE;
184+
}
185+
}));
186+
}
187+
188+
@Test
189+
public void testDaoContextOnCF() {
190+
Assert.assertTrue( SafeFunction.get( () -> {
191+
try (CloseableDAOContext context = CloseableDAOContextAbstract.newCloseableDAOContextCF(
192+
ConnectionFactoryImpl.newInstance( new org.hsqldb.jdbcDriver(), "jdbc:hsqldb:mem:base_db_daocontext1", "testuser", "testp" )
193+
);
194+
Connection conn = context.getConnection()) {
195+
return Boolean.TRUE;
196+
}
197+
}));
198+
}
174199

175200
}

0 commit comments

Comments
 (0)