Author: smarlow(a)redhat.com
Date: 2009-12-09 19:33:22 -0500 (Wed, 09 Dec 2009)
New Revision: 18189
Modified:
core/trunk/core/src/main/java/org/hibernate/PessimisticLockException.java
core/trunk/core/src/main/java/org/hibernate/dialect/DerbyDialect.java
core/trunk/core/src/main/java/org/hibernate/dialect/Dialect.java
core/trunk/core/src/main/java/org/hibernate/dialect/lock/PessimisticReadSelectLockingStrategy.java
core/trunk/core/src/main/java/org/hibernate/dialect/lock/PessimisticReadUpdateLockingStrategy.java
core/trunk/core/src/main/java/org/hibernate/dialect/lock/PessimisticWriteSelectLockingStrategy.java
core/trunk/core/src/main/java/org/hibernate/dialect/lock/PessimisticWriteUpdateLockingStrategy.java
Log:
HHH-4546 JPA-2.0 locking support. Derby read and write pessimistic lock
Modified: core/trunk/core/src/main/java/org/hibernate/PessimisticLockException.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/PessimisticLockException.java 2009-12-09
21:41:12 UTC (rev 18188)
+++ core/trunk/core/src/main/java/org/hibernate/PessimisticLockException.java 2009-12-10
00:33:22 UTC (rev 18189)
@@ -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 @@
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;
Modified: core/trunk/core/src/main/java/org/hibernate/dialect/DerbyDialect.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/dialect/DerbyDialect.java 2009-12-09
21:41:12 UTC (rev 18188)
+++ core/trunk/core/src/main/java/org/hibernate/dialect/DerbyDialect.java 2009-12-10
00:33:22 UTC (rev 18189)
@@ -121,6 +121,19 @@
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/>
Modified: core/trunk/core/src/main/java/org/hibernate/dialect/Dialect.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/dialect/Dialect.java 2009-12-09 21:41:12
UTC (rev 18188)
+++ core/trunk/core/src/main/java/org/hibernate/dialect/Dialect.java 2009-12-10 00:33:22
UTC (rev 18189)
@@ -978,9 +978,15 @@
*/
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 @@
* @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();
}
@@ -1024,6 +1036,31 @@
}
/**
+ * 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?
*
* @return True if the database supports <tt>FOR UPDATE OF</tt> syntax;
Modified:
core/trunk/core/src/main/java/org/hibernate/dialect/lock/PessimisticReadSelectLockingStrategy.java
===================================================================
---
core/trunk/core/src/main/java/org/hibernate/dialect/lock/PessimisticReadSelectLockingStrategy.java 2009-12-09
21:41:12 UTC (rev 18188)
+++
core/trunk/core/src/main/java/org/hibernate/dialect/lock/PessimisticReadSelectLockingStrategy.java 2009-12-10
00:33:22 UTC (rev 18189)
@@ -27,12 +27,13 @@
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 @@
}
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);
}
}
Modified:
core/trunk/core/src/main/java/org/hibernate/dialect/lock/PessimisticReadUpdateLockingStrategy.java
===================================================================
---
core/trunk/core/src/main/java/org/hibernate/dialect/lock/PessimisticReadUpdateLockingStrategy.java 2009-12-09
21:41:12 UTC (rev 18188)
+++
core/trunk/core/src/main/java/org/hibernate/dialect/lock/PessimisticReadUpdateLockingStrategy.java 2009-12-10
00:33:22 UTC (rev 18189)
@@ -28,16 +28,17 @@
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 @@
}
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);
}
}
Modified:
core/trunk/core/src/main/java/org/hibernate/dialect/lock/PessimisticWriteSelectLockingStrategy.java
===================================================================
---
core/trunk/core/src/main/java/org/hibernate/dialect/lock/PessimisticWriteSelectLockingStrategy.java 2009-12-09
21:41:12 UTC (rev 18188)
+++
core/trunk/core/src/main/java/org/hibernate/dialect/lock/PessimisticWriteSelectLockingStrategy.java 2009-12-10
00:33:22 UTC (rev 18189)
@@ -30,6 +30,7 @@
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 @@
}
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);
}
}
Modified:
core/trunk/core/src/main/java/org/hibernate/dialect/lock/PessimisticWriteUpdateLockingStrategy.java
===================================================================
---
core/trunk/core/src/main/java/org/hibernate/dialect/lock/PessimisticWriteUpdateLockingStrategy.java 2009-12-09
21:41:12 UTC (rev 18188)
+++
core/trunk/core/src/main/java/org/hibernate/dialect/lock/PessimisticWriteUpdateLockingStrategy.java 2009-12-10
00:33:22 UTC (rev 18189)
@@ -28,16 +28,17 @@
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 @@
}
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);
}
}