8000 HHH-6865 - PessimisticLockException should be thrown when pessimistic… · JavaInCloud/hibernate-orm@eb59e81 · GitHub
[go: up one dir, main page]

Skip to content

Commit eb59e81

Browse files
committed
HHH-6865 - PessimisticLockException should be thrown when pessimistic read and write locking strategies fail
1 parent 3577cbd commit eb59e81

17 files changed

+328
-246
lines changed
Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/*
22
* Hibernate, Relational Persistence for Idiomatic Java
33
*
4-
* Copyright (c) 2009, Red Hat Middleware LLC or third-party contributors as
4+
* Copyright (c) 2009-2011, Red Hat Inc. or third-party contributors as
55
* indicated by the @author tags or express copyright attribution
66
* statements applied by the authors. All third-party contributions are
7-
* distributed under license by Red Hat Middleware LLC.
7+
* distributed under license by Red Hat Inc.
88
*
99
* This copyrighted material is made available to anyone wishing to use, modify,
1010
* copy, or redistribute it subject to the terms and conditions of the GNU
@@ -20,33 +20,20 @@
2020
* Free Software Foundation, Inc.
2121
* 51 Franklin Street, Fifth Floor
2222
* Boston, MA 02110-1301 USA
23-
*
2423
*/
2524
package org.hibernate;
2625

26+
import org.hibernate.dialect.lock.OptimisticEntityLockException;
2727

2828
/**
29-
*
3029
* Throw when an optimistic locking conflict occurs.
3130
*
3231
* @author Scott Marlow
32+
*
33+
* @deprecated Use {@link org.hibernate.dialect.lock.OptimisticEntityLockException} instead
3334
*/
34-
public class OptimisticLockException extends HibernateException {
35-
36-
Object entity;
37-
38-
public OptimisticLockException(String s) {
39-
super(s);
35+
public class OptimisticLockException extends OptimisticEntityLockException {
36+
public OptimisticLockException(Object entity, String message) {
37+
super( entity, message );
4038
}
41-
42-
public OptimisticLockException(String s, Object entity) {
43-
super(s);
44-
this.entity = entity;
45-
}
46-
47-
public Object getEntity() {
48-
return entity;
49-
}
50-
51-
5239
}
Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/*
22
* Hibernate, Relational Persistence for Idiomatic Java
33
*
4-
* Copyright (c) 2009, Red Hat Middleware LLC or third-party contributors as
4+
* Copyright (c) 2009-2011, Red Hat Inc. or third-party contributors as
55
* indicated by the @author tags or express copyright attribution
66
* statements applied by the authors. All third-party contributions are
7-
* distributed under license by Red Hat Middleware LLC.
7+
* distributed under license by Red Hat Inc.
88
*
99
* This copyrighted material is made available to anyone wishing to use, modify,
1010
* copy, or redistribute it subject to the terms and conditions of the GNU
@@ -20,39 +20,18 @@
2020
* Free Software Foundation, Inc.
2121
* 51 Franklin Street, Fifth Floor
2222
* Boston, MA 02110-1301 USA
23-
*
2423
*/
2524
package org.hibernate;
25+
2626
import java.sql.SQLException;
2727

2828
/**
29-
*
3029
* Thrown when a pessimistic locking conflict occurs.
3130
*
3231
* @author Scott Marlow
3332
*/
3433
public class PessimisticLockException extends JDBCException {
35-
36-
Object entity;
37-
38-
39-
public PessimisticLockException(String s, JDBCException je, Object entity) {
40-
super(s, je.getSQLException());
41-
this.entity = entity;
42-
}
43-
44-
public PessimisticLockException(String s, SQLException se, Object entity) {
45-
super(s, se);
46-
this.entity = entity;
47-
}
48-
4934
public PessimisticLockException(String s, SQLException se, String sql) {
50-
super(s, se, sql);
51-
this.entity = null;
52-
}
53-
54-
public Object getEntity() {
55-
return entity;
35+
super( s, se, sql );
5636
}
57-
5837
}

hibernate-core/src/main/java/org/hibernate/action/internal/EntityVerifyVersionProcess.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public void doBeforeTransactionCompletion(SessionImplementor session) {
5252
Object latestVersion = persister.getCurrentVersion( entry.getId(), session );
5353
if ( !entry.getVersion().equals( latestVersion ) ) {
5454
throw new OptimisticLockException(
55+
object,
5556
"Newer version [" + latestVersion +
5657
"] of entity [" + MessageHelper.infoString( entry.getEntityName(), entry.getId() ) +
5758
"] found in database"

hibernate-core/src/main/java/org/hibernate/dialect/lock/LockingStrategy.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ public interface LockingStrategy {
5151
* @param object The object logically being locked (currently not used)
5252
* @param timeout timeout in milliseconds, 0 = no wait, -1 = wait indefinitely
5353
* @param session The session from which the lock request originated
54-
* @throws StaleObjectStateException Indicates an optimistic lock failure
55-
* as part of acquiring the requested database lock.
56-
* @throws JDBCException Indicates errors from the <tt>JDBC</tt> driver.
54+
* @throws StaleObjectStateException Indicates an inability to locate the database row as part of acquiring
55+
* the requested lock.
56+
* @throws LockingStrategyException Indicates a failure in the lock attempt
5757
*/
5858
public void lock(Serializable id, Object version, Object object, int timeout, SessionImplementor session)
59-
throws StaleObjectStateException, JDBCException;
59+
throws StaleObjectStateException, LockingStrategyException;
6060
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2011, 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.dialect.lock;
25+
26+
import org.hibernate.HibernateException;
27+
28+
/**
29+
* Represents an error trying to apply a {@link LockingStrategy} to an entity
30+
*
31+
* @author Steve Ebersole
32+
*/
33+
public abstract class LockingStrategyException extends HibernateException {
34+
private final Object entity;
35+
36+
public LockingStrategyException(Object entity, String message) {
37+
super( message );
38+
this.entity = entity;
39+
}
40+
41+
public LockingStrategyException(Object entity, String message, Throwable root) {
42+
super( message, root );
43+
this.entity = entity;
44+
}
45+
46+
public Object getEntity() {
47+
return entity;
48+
}
49+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2011, 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.dialect.lock;
25+
26+
/**
27+
* Represents an error trying to apply an optimistic {@link LockingStrategy} to an entity
28+
*
29+
* @author Steve Ebersole
30+
*/
31+
public class OptimisticEntityLockException extends LockingStrategyException {
32+
public OptimisticEntityLockException(Object entity, String message) {
33+
super( entity, message );
34+
}
35+
36+
public OptimisticEntityLockException(Object entity, String message, Throwable root) {
37+
super( entity, message, root );
38+
}
39+
}

hibernate-core/src/main/java/org/hibernate/dialect/lock/OptimisticForceIncrementLockingStrategy.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@
2222
* Boston, MA 02110-1301 USA
2323
*/
2424
package org.hibernate.dialect.lock;
25+
2526
import java.io.Serializable;
2627

2728
import org.hibernate.HibernateException;
28-
import org.hibernate.JDBCException;
2929
import org.hibernate.LockMode;
30-
import org.hibernate.StaleObjectStateException;
3130
import org.hibernate.action.internal.EntityIncrementVersionProcess;
3231
import org.hibernate.engine.spi.EntityEntry;
3332
import org.hibernate.engine.spi.SessionImplementor;
@@ -61,14 +60,8 @@ public OptimisticForceIncrementLockingStrategy(Lockable lockable, LockMode lockM
6160
}
6261
}
6362

64-
/**
65-
* @see LockingStrategy#lock
66-
*/
67-
public void lock(
68-
Serializable id,
69-
Object version,
70-
Object object,
71-
int timeout, SessionImplementor session) throws StaleObjectStateException, JDBCException {
63+
@Override
64+
public void lock(Serializable id, Object version, Object object, int timeout, SessionImplementor session) {
7265
if ( !lockable.isVersioned() ) {
7366
throw new HibernateException( "[" + lockMode + "] not supported for non-versioned entities [" + lockable.getEntityName() + "]" );
7467
}

hibernate-core/src/main/java/org/hibernate/dialect/lock/OptimisticLockingStrategy.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* Boston, MA 02110-1301 USA
2323
*/
2424
package org.hibernate.dialect.lock;
25+
2526
import java.io.Serializable;
2627

2728
import org.hibernate.HibernateException;
@@ -40,9 +41,8 @@
4041
* <p/>
4142
* This strategy is valid for LockMode.OPTIMISTIC
4243
*
43-
* @since 3.5
44-
*
4544
* @author Scott Marlow
45+
* @since 3.5
4646
*/
4747
public class OptimisticLockingStrategy implements LockingStrategy {
4848

@@ -53,7 +53,7 @@ public class OptimisticLockingStrategy implements LockingStrategy {
5353
* Construct locking strategy.
5454
*
5555
* @param lockable The metadata for the entity to be locked.
56-
* @param lockMode Indictates the type of lock to be acquired.
56+
* @param lockMode Indicates the type of lock to be acquired.
5757
*/
5858
public OptimisticLockingStrategy(Lockable lockable, LockMode lockMode) {
5959
this.lockable = lockable;
@@ -63,16 +63,10 @@ public OptimisticLockingStrategy(Lockable lockable, LockMode lockMode) {
6363
}
6464
}
6565

66-
/**
67-
* @see org.hibernate.dialect.lock.LockingStrategy#lock
68-
*/
69-
public void lock(
70-
Serializable id,
71-
Object version,
72-
Object object,
73-
int timeout, SessionImplementor session) throws StaleObjectStateException, JDBCException {
66+
@Override
67+
public void lock(Serializable id, Object version, Object object, int timeout, SessionImplementor session) {
7468
if ( !lockable.isVersioned() ) {
75-
throw new OptimisticLockException( "[" + lockMode + "] not supported for non-versioned entities [" + lockable.getEntityName() + "]" );
69+
throw new OptimisticLockException( object, "[" + lockMode + "] not supported for non-versioned entities [" + lockable.getEntityName() + "]" );
7670
}
7771
EntityEntry entry = session.getPersistenceContext().getEntry(object);
7872
EventSource source = (EventSource)session;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2011, 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.dialect.lock;
25+
26+
import org.hibernate.JDBCException;
27+
28+
/**
29+
* Represents an error trying to apply a pessimistic {@link LockingStrategy} to an entity
30+
*
31+
* @author Steve Ebersole
32+
*/
33+
public class PessimisticEntityLockException extends LockingStrategyException {
34+
public PessimisticEntityLockException(Object entity, String message, JDBCException root) {
35+
super( entity, message, root );
36+
}
37+
}

hibernate-core/src/main/java/org/hibernate/dialect/lock/PessimisticForceIncrementLockingStrategy.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@
2222
* Boston, MA 02110-1301 USA
2323
*/
2424
package org.hibernate.dialect.lock;
25+
2526
import java.io.Serializable;
2627

2728
import org.hibernate.HibernateException;
28-
import org.hibernate.JDBCException;
2929
import org.hibernate.LockMode;
30-
import org.hibernate.StaleObjectStateException;
3130
import org.hibernate.engine.spi.EntityEntry;
3231
import org.hibernate.engine.spi.SessionImplementor;
3332
import org.hibernate.persister.entity.EntityPersister;
@@ -60,15 +59,8 @@ public PessimisticForceIncrementLockingStrategy(Lockable lockable, LockMode lock
6059
}
6160
}
6261

63-
/**
64-
* {@inheritDoc}
65-
*/
66-
public void lock(
67-
Serializable id,
68-
Object version,
69-
Object object,
70-
int timeout,
71-
SessionImplementor session) throws StaleObjectStateException, JDBCException {
62+
@Override
63+
public void lock(Serializable id, Object version, Object object, int timeout, SessionImplementor session) {
7264
if ( !lockable.isVersioned() ) {
7365
throw new HibernateException( "[" + lockMode + "] not supported for non-versioned entities [" + lockable.getEntityName() + "]" );
7466
}

0 commit comments

Comments
 (0)
0