HHH-11120 - SessionFactoryOptionsImpl#releaseResourcesOnCloseEnabled never initialized
This commit is contained in:
parent
d4d2b8f74f
commit
348f92b340
|
@ -137,6 +137,7 @@ public class SessionFactoryOptionsImpl implements SessionFactoryOptions {
|
|||
|
||||
this.flushBeforeCompletionEnabled = state.isFlushBeforeCompletionEnabled();
|
||||
this.autoCloseSessionEnabled = state.isAutoCloseSessionEnabled();
|
||||
this.releaseResourcesOnCloseEnabled = state.isReleaseResourcesOnCloseEnabled();
|
||||
|
||||
this.jtaTrackByThread = state.isJtaTrackByThread();
|
||||
this.preferUserTransaction = state.isPreferUserTransaction();
|
||||
|
|
|
@ -0,0 +1,76 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.jpa.test.ejb3configuration;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Map;
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
import org.hibernate.jpa.AvailableSettings;
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
import org.hibernate.jpa.test.Wallet;
|
||||
|
||||
import org.hibernate.test.util.jdbc.PreparedStatementSpyConnectionProvider;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
public class DisableDiscardPersistenceContextOnCloseTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider();
|
||||
|
||||
@Override
|
||||
protected Map getConfig() {
|
||||
Map config = super.getConfig();
|
||||
config.put( AvailableSettings.DISCARD_PC_ON_CLOSE, "false");
|
||||
config.put(
|
||||
org.hibernate.cfg.AvailableSettings.CONNECTION_PROVIDER,
|
||||
connectionProvider
|
||||
);
|
||||
return config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void releaseResources() {
|
||||
super.releaseResources();
|
||||
connectionProvider.stop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class[] getAnnotatedClasses() {
|
||||
return new Class[]{
|
||||
Wallet.class,
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDiscardOnClose() throws SQLException {
|
||||
EntityManager em = entityManagerFactory().createEntityManager();
|
||||
Wallet wallet = new Wallet();
|
||||
wallet.setSerial( "123" );
|
||||
|
||||
try {
|
||||
em.getTransaction().begin();
|
||||
em.persist( wallet );
|
||||
assertEquals( 1, connectionProvider.getAcquiredConnections().size() );
|
||||
em.close();
|
||||
assertEquals( 1, connectionProvider.getAcquiredConnections().size() );
|
||||
assertTrue(em.getTransaction().isActive());
|
||||
}
|
||||
finally {
|
||||
assertEquals( 1, connectionProvider.getAcquiredConnections().size() );
|
||||
em.getTransaction().rollback();
|
||||
assertEquals( 0, connectionProvider.getAcquiredConnections().size() );
|
||||
assertFalse(em.getTransaction().isActive());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.jpa.test.ejb3configuration;
|
||||
|
||||
import java.util.Map;
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
import org.hibernate.jpa.AvailableSettings;
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
import org.hibernate.jpa.test.Wallet;
|
||||
|
||||
import org.hibernate.test.util.jdbc.PreparedStatementSpyConnectionProvider;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
public class EnableDiscardPersistenceContextOnCloseTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider();
|
||||
|
||||
@Override
|
||||
protected Map getConfig() {
|
||||
Map config = super.getConfig();
|
||||
config.put( AvailableSettings.DISCARD_PC_ON_CLOSE, "true");
|
||||
config.put(
|
||||
org.hibernate.cfg.AvailableSettings.CONNECTION_PROVIDER,
|
||||
connectionProvider
|
||||
);
|
||||
return config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void releaseResources() {
|
||||
super.releaseResources();
|
||||
connectionProvider.stop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class[] getAnnotatedClasses() {
|
||||
return new Class[]{
|
||||
Wallet.class,
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDiscardOnClose() {
|
||||
EntityManager em = entityManagerFactory().createEntityManager();
|
||||
Wallet wallet = new Wallet();
|
||||
wallet.setSerial( "123" );
|
||||
|
||||
try {
|
||||
em.getTransaction().begin();
|
||||
em.persist( wallet );
|
||||
assertEquals( 1, connectionProvider.getAcquiredConnections().size() );
|
||||
em.close();
|
||||
assertEquals( 0, connectionProvider.getAcquiredConnections().size() );
|
||||
assertTrue(em.getTransaction().isActive());
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
em.getTransaction().rollback();
|
||||
fail("Should throw IllegalStateException because the Connection is already closed!");
|
||||
}
|
||||
catch ( IllegalStateException expected ) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -34,10 +34,19 @@ public class PreparedStatementSpyConnectionProvider
|
|||
|
||||
private final Map<PreparedStatement, String> preparedStatementMap = new LinkedHashMap<>();
|
||||
|
||||
private final List<Connection> acquiredConnections = new ArrayList<>( );
|
||||
|
||||
@Override
|
||||
public Connection getConnection() throws SQLException {
|
||||
Connection connection = super.getConnection();
|
||||
return spy( connection );
|
||||
Connection connection = spy( super.getConnection() );
|
||||
acquiredConnections.add( connection );
|
||||
return connection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeConnection(Connection conn) throws SQLException {
|
||||
acquiredConnections.remove( conn );
|
||||
super.closeConnection( conn );
|
||||
}
|
||||
|
||||
private Connection spy(Connection connection) {
|
||||
|
@ -64,6 +73,7 @@ public class PreparedStatementSpyConnectionProvider
|
|||
* Clears the recorded PreparedStatements and reset the associated Mocks.
|
||||
*/
|
||||
public void clear() {
|
||||
acquiredConnections.clear();
|
||||
preparedStatementMap.keySet().forEach( Mockito::reset );
|
||||
preparedStatementMap.clear();
|
||||
}
|
||||
|
@ -113,4 +123,12 @@ public class PreparedStatementSpyConnectionProvider
|
|||
public List<PreparedStatement> getPreparedStatements() {
|
||||
return new ArrayList<>( preparedStatementMap.keySet() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of current acquired Connections.
|
||||
* @return list of current acquired Connections
|
||||
*/
|
||||
public List<Connection> getAcquiredConnections() {
|
||||
return acquiredConnections;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue