Skip to content

Commit

Permalink
Added CloseableDAOContextAbstract.newCloseableDAOContextDS()
Browse files Browse the repository at this point in the history
Added CloseableDAOContextAbstract.newCloseableDAOContextCF()
  • Loading branch information
fugerit79 committed Nov 16, 2024
1 parent 3ebe8ac commit 587a185
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Object> attributes;
Expand All @@ -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 );
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

public interface DAOContext extends AttributesHolder {

public Connection getConnection() throws DAOException;
Connection getConnection() throws DAOException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}));
}

}

0 comments on commit 587a185

Please sign in to comment.