HHH-12075 : Update test to work in 5.1
This commit is contained in:
parent
ea497561c3
commit
1ee2e9d1f5
|
@ -44,8 +44,15 @@ class BatchCountingPreparedStatementObserver extends BasicPreparedStatementObser
|
||||||
return batchesAddedByPreparedStatement.get( preparedStatement );
|
return batchesAddedByPreparedStatement.get( preparedStatement );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void connectionProviderStopped() {
|
public void connectionProviderStopped() {
|
||||||
super.connectionProviderStopped();
|
super.connectionProviderStopped();
|
||||||
batchesAddedByPreparedStatement.clear();
|
batchesAddedByPreparedStatement.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clear() {
|
||||||
|
super.clear();
|
||||||
|
batchesAddedByPreparedStatement.clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,36 +6,39 @@
|
||||||
*/
|
*/
|
||||||
package org.hibernate.test.querytimeout;
|
package org.hibernate.test.querytimeout;
|
||||||
|
|
||||||
import java.sql.SQLException;
|
import java.lang.reflect.Method;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
import javax.persistence.QueryHint;
|
|
||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
|
|
||||||
|
import org.hibernate.SQLQuery;
|
||||||
|
import org.hibernate.Session;
|
||||||
import org.hibernate.cfg.AvailableSettings;
|
import org.hibernate.cfg.AvailableSettings;
|
||||||
import org.hibernate.jpa.QueryHints;
|
import org.hibernate.Query;
|
||||||
import org.hibernate.query.NativeQuery;
|
|
||||||
import org.hibernate.query.Query;
|
|
||||||
|
|
||||||
import org.hibernate.testing.TestForIssue;
|
import org.hibernate.testing.TestForIssue;
|
||||||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
||||||
import org.hibernate.test.util.jdbc.PreparedStatementSpyConnectionProvider;
|
import org.hibernate.test.util.jdbc.BasicPreparedStatementObserver;
|
||||||
|
import org.hibernate.test.util.jdbc.PreparedStatementProxyConnectionProvider;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
import static org.mockito.Mockito.times;
|
|
||||||
import static org.mockito.Mockito.verify;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Gail Badner
|
* @author Gail Badner
|
||||||
*/
|
*/
|
||||||
public class QueryTimeOutTest extends BaseNonConfigCoreFunctionalTestCase {
|
public class QueryTimeOutTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
|
|
||||||
private static final PreparedStatementSpyConnectionProvider CONNECTION_PROVIDER = new PreparedStatementSpyConnectionProvider();
|
private static final TimeoutPreparedStatementObserver preparedStatementObserver = new TimeoutPreparedStatementObserver();
|
||||||
|
private static final PreparedStatementProxyConnectionProvider connectionProvider = new PreparedStatementProxyConnectionProvider(
|
||||||
|
preparedStatementObserver
|
||||||
|
);
|
||||||
|
|
||||||
private static final String QUERY = "update AnEntity set name='abc'";
|
private static final String QUERY = "update AnEntity set name='abc'";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -45,126 +48,52 @@ public class QueryTimeOutTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void addSettings(Map settings) {
|
protected void addSettings(Map settings) {
|
||||||
settings.put( AvailableSettings.CONNECTION_PROVIDER, CONNECTION_PROVIDER );
|
settings.put( AvailableSettings.CONNECTION_PROVIDER, connectionProvider );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void before() {
|
public void before() {
|
||||||
CONNECTION_PROVIDER.clear();
|
preparedStatementObserver.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void releaseResources() {
|
||||||
|
super.releaseResources();
|
||||||
|
connectionProvider.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestForIssue( jiraKey = "HHH-12075")
|
@TestForIssue( jiraKey = "HHH-12075")
|
||||||
public void testCreateQuerySetTimeout() {
|
public void testCreateQuerySetTimeout() {
|
||||||
doInHibernate(
|
Session session = openSession();
|
||||||
this::sessionFactory, session -> {
|
session.getTransaction().begin();
|
||||||
|
{
|
||||||
Query query = session.createQuery( QUERY );
|
Query query = session.createQuery( QUERY );
|
||||||
query.setTimeout( 123 );
|
query.setTimeout( 123 );
|
||||||
query.executeUpdate();
|
query.executeUpdate();
|
||||||
|
|
||||||
try {
|
PreparedStatement preparedStatement = preparedStatementObserver.getPreparedStatement( QUERY );
|
||||||
verify( CONNECTION_PROVIDER.getPreparedStatement( QUERY ), times( 1 ) ).setQueryTimeout( 123 );
|
assertEquals( 123, preparedStatementObserver.getTimeOut( preparedStatement ) );
|
||||||
}
|
}
|
||||||
catch (SQLException ex) {
|
session.getTransaction().commit();
|
||||||
fail( "should not have thrown exception" );
|
session.close();
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@TestForIssue( jiraKey = "HHH-12075")
|
|
||||||
public void testCreateQuerySetTimeoutHint() {
|
|
||||||
doInHibernate(
|
|
||||||
this::sessionFactory, session -> {
|
|
||||||
Query query = session.createQuery( QUERY );
|
|
||||||
query.setHint( QueryHints.SPEC_HINT_TIMEOUT, 123000 );
|
|
||||||
query.executeUpdate();
|
|
||||||
|
|
||||||
try {
|
|
||||||
verify( CONNECTION_PROVIDER.getPreparedStatement( QUERY ), times( 1 ) ).setQueryTimeout( 123 );
|
|
||||||
}
|
|
||||||
catch (SQLException ex) {
|
|
||||||
fail( "should not have thrown exception" );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@TestForIssue( jiraKey = "HHH-12075")
|
|
||||||
public void testCreateNativeQuerySetTimeout() {
|
|
||||||
doInHibernate(
|
|
||||||
this::sessionFactory, session -> {
|
|
||||||
NativeQuery query = session.createNativeQuery( QUERY );
|
|
||||||
query.setTimeout( 123 );
|
|
||||||
query.executeUpdate();
|
|
||||||
|
|
||||||
try {
|
|
||||||
verify( CONNECTION_PROVIDER.getPreparedStatement( QUERY ), times( 1 ) ).setQueryTimeout( 123 );
|
|
||||||
}
|
|
||||||
catch (SQLException ex) {
|
|
||||||
fail( "should not have thrown exception" );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@TestForIssue( jiraKey = "HHH-12075")
|
|
||||||
public void testCreateNativeQuerySetTimeoutHint() {
|
|
||||||
doInHibernate(
|
|
||||||
this::sessionFactory, session -> {
|
|
||||||
NativeQuery query = session.createNativeQuery( QUERY );
|
|
||||||
query.setHint( QueryHints.SPEC_HINT_TIMEOUT, 123000 );
|
|
||||||
query.executeUpdate();
|
|
||||||
|
|
||||||
try {
|
|
||||||
verify( CONNECTION_PROVIDER.getPreparedStatement( QUERY ), times( 1 ) ).setQueryTimeout( 123 );
|
|
||||||
}
|
|
||||||
catch (SQLException ex) {
|
|
||||||
fail( "should not have thrown exception" );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestForIssue( jiraKey = "HHH-12075")
|
@TestForIssue( jiraKey = "HHH-12075")
|
||||||
public void testCreateSQLQuerySetTimeout() {
|
public void testCreateSQLQuerySetTimeout() {
|
||||||
doInHibernate(
|
Session session = openSession();
|
||||||
this::sessionFactory, session -> {
|
session.getTransaction().begin();
|
||||||
NativeQuery query = session.createSQLQuery( QUERY );
|
{
|
||||||
query.setTimeout( 123 );
|
SQLQuery query = session.createSQLQuery( QUERY );
|
||||||
query.executeUpdate();
|
query.setTimeout( 123 );
|
||||||
|
query.executeUpdate();
|
||||||
|
|
||||||
try {
|
PreparedStatement preparedStatement = preparedStatementObserver.getPreparedStatement( QUERY );
|
||||||
verify( CONNECTION_PROVIDER.getPreparedStatement( QUERY ), times( 1 ) ).setQueryTimeout( 123 );
|
assertEquals( 123, preparedStatementObserver.getTimeOut( preparedStatement ) );
|
||||||
}
|
}
|
||||||
catch (SQLException ex) {
|
session.getTransaction().commit();
|
||||||
fail( "should not have thrown exception" );
|
session.close();
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@TestForIssue( jiraKey = "HHH-12075")
|
|
||||||
public void testCreateSQLQuerySetTimeoutHint() {
|
|
||||||
doInHibernate(
|
|
||||||
this::sessionFactory, session -> {
|
|
||||||
NativeQuery query = session.createSQLQuery( QUERY );
|
|
||||||
query.setHint( QueryHints.SPEC_HINT_TIMEOUT, 123000 );
|
|
||||||
query.executeUpdate();
|
|
||||||
|
|
||||||
try {
|
|
||||||
verify( CONNECTION_PROVIDER.getPreparedStatement( QUERY ), times( 1 ) ).setQueryTimeout( 123 );
|
|
||||||
}
|
|
||||||
catch (SQLException ex) {
|
|
||||||
fail( "should not have thrown exception" );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Entity(name = "AnEntity" )
|
@Entity(name = "AnEntity" )
|
||||||
|
@ -175,4 +104,46 @@ public class QueryTimeOutTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class TimeoutPreparedStatementObserver extends BasicPreparedStatementObserver {
|
||||||
|
private final Map<PreparedStatement, Integer> timeoutByPreparedStatement =
|
||||||
|
new HashMap<PreparedStatement, Integer>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void preparedStatementMethodInvoked(
|
||||||
|
PreparedStatement preparedStatement,
|
||||||
|
Method method,
|
||||||
|
Object[] args,
|
||||||
|
Object invocationReturnValue) {
|
||||||
|
super.preparedStatementMethodInvoked( preparedStatement, method, args, invocationReturnValue );
|
||||||
|
if ( "setQueryTimeout".equals( method.getName() ) ) {
|
||||||
|
// ugh, when ResourceRegistryStandardImpl closes the PreparedStatement, it calls
|
||||||
|
// PreparedStatement#setQueryTimeout( 0 ). Ignore this call if the PreparedStatement
|
||||||
|
// is already in timeoutByPreparedStatement
|
||||||
|
Integer timeout = (Integer) args[0];
|
||||||
|
Integer existingTimeout = timeoutByPreparedStatement.get( preparedStatement );
|
||||||
|
if ( timeout == 0 && existingTimeout != null && existingTimeout != 0 ) {
|
||||||
|
// ignore;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
timeoutByPreparedStatement.put( preparedStatement, timeout );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getTimeOut(PreparedStatement preparedStatement) {
|
||||||
|
return timeoutByPreparedStatement.get( preparedStatement );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void connectionProviderStopped() {
|
||||||
|
super.connectionProviderStopped();
|
||||||
|
timeoutByPreparedStatement.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clear() {
|
||||||
|
super.clear();
|
||||||
|
timeoutByPreparedStatement.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -66,6 +66,11 @@ public class BasicPreparedStatementObserver implements PreparedStatementObserver
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void connectionProviderStopped() {
|
public void connectionProviderStopped() {
|
||||||
|
clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clear() {
|
||||||
sqlByPreparedStatement.clear();
|
sqlByPreparedStatement.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,4 +63,10 @@ public interface PreparedStatementObserver {
|
||||||
* @return list of recorded PreparedStatements.
|
* @return list of recorded PreparedStatements.
|
||||||
*/
|
*/
|
||||||
List<PreparedStatement> getPreparedStatements();
|
List<PreparedStatement> getPreparedStatements();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clears the recorded PreparedStatements and associated data.
|
||||||
|
*/
|
||||||
|
void clear();
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue