Skip to content

Commit f552f64

Browse files
Sannedreab8
authored andcommitted
HHH-8788 removed unnecessary warning related to follow-on locking with Criteria and LockMode.NONE
1 parent 91aee82 commit f552f64

File tree

2 files changed

+132
-1
lines changed

2 files changed

+132
-1
lines changed

hibernate-core/src/main/java/org/hibernate/loader/criteria/CriteriaLoader.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,12 @@ protected String applyLocks(
201201
Dialect dialect,
202202
List<AfterLoadAction> afterLoadActions) throws QueryException {
203203
final LockOptions lockOptions = parameters.getLockOptions();
204+
204205
if ( lockOptions == null ||
205-
( lockOptions.getLockMode() == LockMode.NONE && lockOptions.getAliasLockCount() == 0 ) ) {
206+
(lockOptions.getLockMode() == LockMode.NONE && (lockOptions.getAliasLockCount() == 0
207+
|| (lockOptions.getAliasLockCount() == 1 && lockOptions
208+
.getAliasSpecificLockMode( "this_" ) == LockMode.NONE)
209+
)) ) {
206210
return sql;
207211
}
208212

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) {DATE}, Red Hat Inc. or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
package org.hibernate.test.criteria;
25+
26+
import org.jboss.byteman.contrib.bmunit.BMRule;
27+
import org.jboss.byteman.contrib.bmunit.BMRules;
28+
import org.jboss.byteman.contrib.bmunit.BMUnitRunner;
29+
30+
import org.hibernate.Criteria;
31+
import org.hibernate.LockMode;
32+
import org.hibernate.Session;
33+
import org.hibernate.Transaction;
34+
35+
import org.junit.Test;
36+
import org.junit.runner.RunWith;
37+
38+
import org.hibernate.testing.TestForIssue;
39+
import org.hibernate.testing.byteman.BytemanHelper;
40+
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
41+
42+
import static org.junit.Assert.assertEquals;
43+
44+
/**
45+
* @author Sanne Grinovero
46+
* @author Andrea Boriero
47+
*/
48+
@TestForIssue(jiraKey = "HHH-8788")
49+
@RunWith(BMUnitRunner.class)
50+
public class CriteriaLockingTest extends BaseCoreFunctionalTestCase {
51+
52+
@Override
53+
protected Class<?>[] getAnnotatedClasses() {
54+
return new Class[] {Bid.class, Item.class};
55+
}
56+
57+
@Test
58+
@BMRules(rules = {
59+
@BMRule(targetClass = "org.hibernate.dialect.Dialect",
60+
targetMethod = "useFollowOnLocking",
61+
action = "return true",
62+
name = "H2DialectUseFollowOnLocking"),
63+
@BMRule(targetClass = "org.hibernate.internal.CoreMessageLogger_$logger",
64+
targetMethod = "usingFollowOnLocking",
65+
helper = "org.hibernate.testing.byteman.BytemanHelper",
66+
action = "countInvocation()",
67+
name = "countWarnings")
68+
})
69+
public void testSetLockModeNONEDoNotLogAWarnMessageWhenTheDialectUseFollowOnLockingIsTrue() {
70+
buildSessionFactory();
71+
final Session s = openSession();
72+
final Transaction tx = s.beginTransaction();
73+
74+
Item item = new Item();
75+
item.name = "ZZZZ";
76+
s.persist( item );
77+
78+
s.flush();
79+
80+
Criteria criteria = s.createCriteria( Item.class )
81+
.setLockMode( LockMode.NONE );
82+
83+
criteria.list();
84+
85+
tx.rollback();
86+
s.close();
87+
88+
releaseSessionFactory();
89+
90+
assertEquals( "HHH000444 should not be called", 0, BytemanHelper.getAndResetInvocationCount() );
91+
}
92+
93+
@Test
94+
@BMRules(rules = {
95+
@BMRule(targetClass = "org.hibernate.dialect.Dialect",
96+
targetMethod = "useFollowOnLocking",
97+
action = "return true",
98+
name = "H2DialectUseFollowOnLocking"),
99+
@BMRule(targetClass = "org.hibernate.internal.CoreMessageLogger_$logger",
100+
targetMethod = "usingFollowOnLocking",
101+
helper = "org.hibernate.testing.byteman.BytemanHelper",
102+
action = "countInvocation()",
103+
name = "countWarnings")
104+
})
105+
public void testSetLockModeDifferentFromNONELogAWarnMessageWhenTheDialectUseFollowOnLockingIsTrue() {
106+
buildSessionFactory();
107+
final Session s = openSession();
108+
final Transaction tx = s.beginTransaction();
109+
110+
Item item = new Item();
111+
item.name = "ZZZZ";
112+
s.persist( item );
113+
114+
s.flush();
115+
116+
Criteria criteria = s.createCriteria( Item.class )
117+
.setLockMode( LockMode.OPTIMISTIC );
118+
119+
criteria.list();
120+
121+
tx.rollback();
122+
s.close();
123+
releaseSessionFactory();
124+
125+
assertEquals( "HHH000444 should not be called", 1, BytemanHelper.getAndResetInvocationCount() );
126+
}
127+
}

0 commit comments

Comments
 (0)