Skip to content

Commit 6866f5e

Browse files
committed
HHH-11997 - EntityManager.createNamedQuery throwing illegalstateexception
1 parent 9fa7959 commit 6866f5e

File tree

2 files changed

+139
-13
lines changed

2 files changed

+139
-13
lines changed

hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -751,23 +751,28 @@ public QueryImplementor createNamedQuery(String name) {
751751
}
752752

753753
protected <T> QueryImplementor<T> buildQueryFromName(String name, Class<T> resultType) {
754-
checkOpen();
755-
checkTransactionSynchStatus();
756-
delayedAfterCompletion();
754+
try {
755+
checkOpen();
756+
checkTransactionSynchStatus();
757+
delayedAfterCompletion();
757758

758-
// todo : apply stored setting at the JPA Query level too
759+
// todo : apply stored setting at the JPA Query level too
759760

760-
final NamedQueryDefinition namedQueryDefinition = getFactory().getNamedQueryRepository().getNamedQueryDefinition( name );
761-
if ( namedQueryDefinition != null ) {
762-
return createQuery( namedQueryDefinition, resultType );
763-
}
761+
final NamedQueryDefinition namedQueryDefinition = getFactory().getNamedQueryRepository().getNamedQueryDefinition( name );
762+
if ( namedQueryDefinition != null ) {
763+
return createQuery( namedQueryDefinition, resultType );
764+
}
764765

765-
final NamedSQLQueryDefinition nativeQueryDefinition = getFactory().getNamedQueryRepository().getNamedSQLQueryDefinition( name );
766-
if ( nativeQueryDefinition != null ) {
767-
return (QueryImplementor<T>) createNativeQuery( nativeQueryDefinition, resultType );
768-
}
766+
final NamedSQLQueryDefinition nativeQueryDefinition = getFactory().getNamedQueryRepository().getNamedSQLQueryDefinition( name );
767+
if ( nativeQueryDefinition != null ) {
768+
return (QueryImplementor<T>) createNativeQuery( nativeQueryDefinition, resultType );
769+
}
769770

770-
throw exceptionConverter.convert( new IllegalArgumentException( "No query defined for that name [" + name + "]" ) );
771+
throw exceptionConverter.convert( new IllegalArgumentException( "No query defined for that name [" + name + "]" ) );
772+
}
773+
catch (RuntimeException e) {
774+
throw !( e instanceof IllegalArgumentException ) ? new IllegalArgumentException( e ) : e;
775+
}
771776
}
772777

773778
@SuppressWarnings({"WeakerAccess", "unchecked"})
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
8+
//$Id$
9+
10+
package org.hibernate.jpa.test;
11+
12+
import java.util.Map;
13+
import javax.persistence.Query;
14+
15+
import org.hibernate.cfg.Environment;
16+
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
17+
import org.hibernate.resource.transaction.spi.TransactionCoordinator;
18+
import org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder;
19+
20+
import org.hibernate.testing.TestForIssue;
21+
import org.junit.Test;
22+
23+
import org.mockito.Mockito;
24+
25+
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
26+
import static org.junit.Assert.assertEquals;
27+
import static org.mockito.ArgumentMatchers.any;
28+
import static org.mockito.ArgumentMatchers.anyBoolean;
29+
import static org.mockito.Mockito.doNothing;
30+
import static org.mockito.Mockito.doThrow;
31+
import static org.mockito.Mockito.when;
32+
33+
/**
34+
* @author Vlad Mihalcea
35+
*/
36+
public class NamedQueryTransactionFailureTest extends BaseEntityManagerFunctionalTestCase {
37+
@Override
38+
public Class[] getAnnotatedClasses() {
39+
return new Class[] {
40+
Item.class,
41+
Distributor.class,
42+
Wallet.class
43+
};
44+
}
45+
46+
private TransactionCoordinator transactionCoordinator;
47+
48+
@SuppressWarnings( {"unchecked"})
49+
protected void addConfigOptions(Map options) {
50+
TransactionCoordinatorBuilder transactionCoordinatorBuilder = Mockito.mock( TransactionCoordinatorBuilder.class);
51+
when(transactionCoordinatorBuilder.getDefaultConnectionHandlingMode())
52+
.thenReturn( PhysicalConnectionHandlingMode.DELAYED_ACQUISITION_AND_HOLD );
53+
54+
when(transactionCoordinatorBuilder.isJta())
55+
.thenReturn( false );
56+
57+
transactionCoordinator = Mockito.mock( TransactionCoordinator.class);
58+
59+
when(transactionCoordinatorBuilder.buildTransactionCoordinator(any(), any( TransactionCoordinatorBuilder.Options.class)))
60+
.thenReturn( transactionCoordinator );
61+
62+
when( transactionCoordinator.getTransactionCoordinatorBuilder() ).thenReturn( transactionCoordinatorBuilder );
63+
64+
TransactionCoordinator.TransactionDriver transactionDriver = Mockito.mock( TransactionCoordinator.TransactionDriver.class);
65+
when( transactionCoordinator.getTransactionDriverControl() ).thenReturn( transactionDriver );
66+
when( transactionCoordinator.isActive() ).thenReturn( true );
67+
when( transactionDriver.isActive( anyBoolean() ) ).thenReturn( false );
68+
69+
doNothing().when( transactionCoordinator ).pulse();
70+
71+
options.put( Environment.TRANSACTION_COORDINATOR_STRATEGY, transactionCoordinatorBuilder );
72+
}
73+
74+
@Override
75+
protected boolean createSchema() {
76+
return false;
77+
}
78+
79+
@Test
80+
@TestForIssue( jiraKey = "HHH-11997" )
81+
public void testNamedQueryWithTransactionSynchStatus() {
82+
try {
83+
doInJPA( this::entityManagerFactory, entityManager -> {
84+
try {
85+
Mockito.reset( transactionCoordinator );
86+
doThrow(IllegalStateException.class).when( transactionCoordinator ).pulse();
87+
88+
entityManager.createNamedQuery( "NamedQuery" );
89+
}
90+
catch (Exception e) {
91+
assertEquals(IllegalArgumentException.class, e.getClass());
92+
assertEquals(IllegalStateException.class, e.getCause().getClass());
93+
}
94+
});
95+
}
96+
catch (Exception ignore) {
97+
}
98+
}
99+
100+
@Test
101+
@TestForIssue( jiraKey = "HHH-11997" )
102+
public void testNamedQueryWithMarkForRollbackOnlyFailure() {
103+
try {
104+
doInJPA( this::entityManagerFactory, entityManager -> {
105+
try {
106+
Mockito.reset( transactionCoordinator );
107+
doNothing().
108+
doThrow(IllegalStateException.class).when( transactionCoordinator ).pulse();
109+
110+
entityManager.createNamedQuery( "NamedQuery" );
111+
}
112+
catch (Exception e) {
113+
assertEquals(IllegalArgumentException.class, e.getClass());
114+
assertEquals(IllegalStateException.class, e.getCause().getClass());
115+
}
116+
});
117+
}
118+
catch (Exception ignore) {
119+
}
120+
}
121+
}

0 commit comments

Comments
 (0)