HHH-4546 JPA-2.0 locking support. Derby read and write pessimistic lock

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18189 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Scott Marlow 2009-12-10 00:33:22 +00:00
parent 77e57b6727
commit cf0e1c6476
7 changed files with 89 additions and 26 deletions

View File

@ -1,7 +1,7 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
* Copyright (c) 2009, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
@ -38,6 +38,11 @@ public class PessimisticLockException extends HibernateException {
super(s);
}
public PessimisticLockException(String s, Throwable throwable, Object entity) {
super(s, throwable);
this.entity = entity;
}
public PessimisticLockException(String s, Object entity) {
super(s);
this.entity = entity;

View File

@ -121,6 +121,19 @@ public class DerbyDialect extends DB2Dialect {
return isTenPointFiveReleaseOrNewer();
}
public String getForUpdateString() {
return " for update with rs";
}
public String getWriteLockString(int timeout) {
return " for update with rs";
}
public String getReadLockString(int timeout) {
return " for read only with rs";
}
/**
* {@inheritDoc}
* <p/>

View File

@ -978,9 +978,15 @@ public abstract class Dialect {
*/
public String getForUpdateString(LockOptions lockOptions) {
LockMode lockMode = lockOptions.getLockMode();
if ( lockMode==LockMode.UPGRADE || lockMode==LockMode.PESSIMISTIC_READ || lockMode==LockMode.PESSIMISTIC_WRITE) {
if ( lockMode==LockMode.UPGRADE) {
return getForUpdateString();
}
else if( lockMode==LockMode.PESSIMISTIC_READ ) {
return getReadLockString(lockOptions.getTimeOut());
}
else if( lockMode==LockMode.PESSIMISTIC_WRITE ) {
return getWriteLockString(lockOptions.getTimeOut());
}
else if ( lockMode==LockMode.UPGRADE_NOWAIT ) {
return getForUpdateNowaitString();
}
@ -999,9 +1005,15 @@ public abstract class Dialect {
* @return The appropriate for update fragment.
*/
public String getForUpdateString(LockMode lockMode) {
if ( lockMode==LockMode.UPGRADE || lockMode==LockMode.PESSIMISTIC_READ || lockMode==LockMode.PESSIMISTIC_WRITE) {
if ( lockMode==LockMode.UPGRADE ) {
return getForUpdateString();
}
else if( lockMode==LockMode.PESSIMISTIC_READ ) {
return getReadLockString(LockOptions.WAIT_FOREVER);
}
else if( lockMode==LockMode.PESSIMISTIC_WRITE ) {
return getWriteLockString(LockOptions.WAIT_FOREVER);
}
else if ( lockMode==LockMode.UPGRADE_NOWAIT ) {
return getForUpdateNowaitString();
}
@ -1023,6 +1035,31 @@ public abstract class Dialect {
return " for update";
}
/**
* Get the string to append to SELECT statements to acquire WRITE locks
* for this dialect. Location of the of the returned string is treated
* the same as getForUpdateString.
*
* @param timeout in milliseconds, -1 for indefinite wait and 0 for no wait.
* @return The appropriate <tt>LOCK</tt> clause string.
*/
public String getWriteLockString(int timeout) {
return getForUpdateString();
}
/**
* Get the string to append to SELECT statements to acquire WRITE locks
* for this dialect. Location of the of the returned string is treated
* the same as getForUpdateString.
*
* @param timeout in milliseconds, -1 for indefinite wait and 0 for no wait.
* @return The appropriate <tt>LOCK</tt> clause string.
*/
public String getReadLockString(int timeout) {
return getForUpdateString();
}
/**
* Is <tt>FOR UPDATE OF</tt> syntax supported?
*

View File

@ -27,12 +27,13 @@ package org.hibernate.dialect.lock;
import org.hibernate.persister.entity.Lockable;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.StaleObjectStateException;
import org.hibernate.JDBCException;
import org.hibernate.LockMode;
import org.hibernate.sql.SimpleSelect;
import org.hibernate.pretty.MessageHelper;
import org.hibernate.exception.JDBCExceptionHelper;
import org.hibernate.LockMode;
import org.hibernate.StaleObjectStateException;
import org.hibernate.JDBCException;
import org.hibernate.PessimisticLockException;
import java.io.Serializable;
import java.sql.PreparedStatement;
@ -117,12 +118,13 @@ public class PessimisticReadSelectLockingStrategy implements LockingStrategy {
}
catch ( SQLException sqle ) {
throw JDBCExceptionHelper.convert(
JDBCException e = JDBCExceptionHelper.convert(
session.getFactory().getSQLExceptionConverter(),
sqle,
"could not lock: " + MessageHelper.infoString( lockable, id, session.getFactory() ),
sql
);
throw new PessimisticLockException("could not obtain pessimistic lock", e, object);
}
}

View File

@ -28,16 +28,17 @@ import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.hibernate.HibernateException;
import org.hibernate.JDBCException;
import org.hibernate.LockMode;
import org.hibernate.StaleObjectStateException;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.exception.JDBCExceptionHelper;
import org.hibernate.persister.entity.Lockable;
import org.hibernate.pretty.MessageHelper;
import org.hibernate.sql.Update;
import org.hibernate.LockMode;
import org.hibernate.HibernateException;
import org.hibernate.StaleObjectStateException;
import org.hibernate.JDBCException;
import org.hibernate.PessimisticLockException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -120,12 +121,13 @@ public class PessimisticReadUpdateLockingStrategy implements LockingStrategy {
}
catch ( SQLException sqle ) {
throw JDBCExceptionHelper.convert(
JDBCException e = JDBCExceptionHelper.convert(
session.getFactory().getSQLExceptionConverter(),
sqle,
"could not lock: " + MessageHelper.infoString( lockable, id, session.getFactory() ),
sql
);
sqle,
"could not lock: " + MessageHelper.infoString( lockable, id, session.getFactory() ),
sql
);
throw new PessimisticLockException("could not obtain pessimistic lock", e, object);
}
}

View File

@ -30,6 +30,7 @@ import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.StaleObjectStateException;
import org.hibernate.JDBCException;
import org.hibernate.LockMode;
import org.hibernate.PessimisticLockException;
import org.hibernate.sql.SimpleSelect;
import org.hibernate.pretty.MessageHelper;
import org.hibernate.exception.JDBCExceptionHelper;
@ -117,12 +118,13 @@ public class PessimisticWriteSelectLockingStrategy implements LockingStrategy {
}
catch ( SQLException sqle ) {
throw JDBCExceptionHelper.convert(
JDBCException e = JDBCExceptionHelper.convert(
session.getFactory().getSQLExceptionConverter(),
sqle,
"could not lock: " + MessageHelper.infoString( lockable, id, session.getFactory() ),
sql
);
throw new PessimisticLockException("could not obtain pessimistic lock", e, object);
}
}

View File

@ -28,16 +28,17 @@ import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.hibernate.HibernateException;
import org.hibernate.JDBCException;
import org.hibernate.LockMode;
import org.hibernate.StaleObjectStateException;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.exception.JDBCExceptionHelper;
import org.hibernate.persister.entity.Lockable;
import org.hibernate.pretty.MessageHelper;
import org.hibernate.sql.Update;
import org.hibernate.LockMode;
import org.hibernate.HibernateException;
import org.hibernate.StaleObjectStateException;
import org.hibernate.JDBCException;
import org.hibernate.PessimisticLockException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -120,12 +121,13 @@ public class PessimisticWriteUpdateLockingStrategy implements LockingStrategy {
}
catch ( SQLException sqle ) {
throw JDBCExceptionHelper.convert(
JDBCException e = JDBCExceptionHelper.convert(
session.getFactory().getSQLExceptionConverter(),
sqle,
"could not lock: " + MessageHelper.infoString( lockable, id, session.getFactory() ),
sql
);
sqle,
"could not lock: " + MessageHelper.infoString( lockable, id, session.getFactory() ),
sql
);
throw new PessimisticLockException("could not obtain pessimistic lock", e, object);
}
}