HHH-11120 - SessionFactoryOptionsImpl#releaseResourcesOnCloseEnabled never initialized

This commit is contained in:
Boris Byk 2016-09-20 22:06:16 -07:00 committed by Vlad Mihalcea
parent d4d2b8f74f
commit 348f92b340
4 changed files with 174 additions and 2 deletions

View File

@ -137,6 +137,7 @@ public class SessionFactoryOptionsImpl implements SessionFactoryOptions {
this.flushBeforeCompletionEnabled = state.isFlushBeforeCompletionEnabled(); this.flushBeforeCompletionEnabled = state.isFlushBeforeCompletionEnabled();
this.autoCloseSessionEnabled = state.isAutoCloseSessionEnabled(); this.autoCloseSessionEnabled = state.isAutoCloseSessionEnabled();
this.releaseResourcesOnCloseEnabled = state.isReleaseResourcesOnCloseEnabled();
this.jtaTrackByThread = state.isJtaTrackByThread(); this.jtaTrackByThread = state.isJtaTrackByThread();
this.preferUserTransaction = state.isPreferUserTransaction(); this.preferUserTransaction = state.isPreferUserTransaction();

View File

@ -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());
}
}
}

View File

@ -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 ) {
}
}
}
}

View File

@ -34,10 +34,19 @@ public class PreparedStatementSpyConnectionProvider
private final Map<PreparedStatement, String> preparedStatementMap = new LinkedHashMap<>(); private final Map<PreparedStatement, String> preparedStatementMap = new LinkedHashMap<>();
private final List<Connection> acquiredConnections = new ArrayList<>( );
@Override @Override
public Connection getConnection() throws SQLException { public Connection getConnection() throws SQLException {
Connection connection = super.getConnection(); Connection connection = spy( super.getConnection() );
return spy( connection ); acquiredConnections.add( connection );
return connection;
}
@Override
public void closeConnection(Connection conn) throws SQLException {
acquiredConnections.remove( conn );
super.closeConnection( conn );
} }
private Connection spy(Connection connection) { private Connection spy(Connection connection) {
@ -64,6 +73,7 @@ public class PreparedStatementSpyConnectionProvider
* Clears the recorded PreparedStatements and reset the associated Mocks. * Clears the recorded PreparedStatements and reset the associated Mocks.
*/ */
public void clear() { public void clear() {
acquiredConnections.clear();
preparedStatementMap.keySet().forEach( Mockito::reset ); preparedStatementMap.keySet().forEach( Mockito::reset );
preparedStatementMap.clear(); preparedStatementMap.clear();
} }
@ -113,4 +123,12 @@ public class PreparedStatementSpyConnectionProvider
public List<PreparedStatement> getPreparedStatements() { public List<PreparedStatement> getPreparedStatements() {
return new ArrayList<>( preparedStatementMap.keySet() ); return new ArrayList<>( preparedStatementMap.keySet() );
} }
/**
* Get a list of current acquired Connections.
* @return list of current acquired Connections
*/
public List<Connection> getAcquiredConnections() {
return acquiredConnections;
}
} }