Skip to content

Commit

Permalink
HSEARCH-996 Update Hibernate Core to 4.0.0.CR7
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanne authored and hferentschik committed Dec 1, 2011
1 parent 160c016 commit d03f928
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,25 +215,22 @@ else if ( e instanceof TransientObjectException ) {
}

public PersistenceException wrapLockException(HibernateException e, LockOptions lockOptions) {
PersistenceException pe;
if ( e instanceof org.hibernate.OptimisticLockException ) {
org.hibernate.OptimisticLockException ole = (org.hibernate.OptimisticLockException) e;
pe = new OptimisticLockException( ole.getMessage(), ole, ole.getEntity() );
if ( OptimisticLockingCompatibilityHelper.isOptimisticLockException( e ) ) {
throw OptimisticLockingCompatibilityHelper.convertToLockException( e );
}
else if ( e instanceof org.hibernate.PessimisticLockException ) {
org.hibernate.PessimisticLockException ple = (org.hibernate.PessimisticLockException) e;
if ( lockOptions != null && lockOptions.getTimeOut() > -1 ) {
// assume lock timeout occurred if a timeout or NO WAIT was specified
pe = new LockTimeoutException( ple.getMessage(), ple, ple.getEntity() );
return new LockTimeoutException( ple.getMessage(), ple );
}
else {
pe = new PessimisticLockException( ple.getMessage(), ple, ple.getEntity() );
return new PessimisticLockException( ple.getMessage(), ple );
}
}
else {
pe = new OptimisticLockException( e.getMessage(), e );
return new OptimisticLockException( e.getMessage(), e );
}
return pe;
}

void throwPersistenceException(PersistenceException e) {
Expand All @@ -245,28 +242,26 @@ void throwPersistenceException(PersistenceException e) {

@SuppressWarnings( { "ThrowableInstanceNeverThrown" })
PersistenceException wrapStaleStateException(StaleStateException e) {
PersistenceException pe;
if ( e instanceof StaleObjectStateException ) {
StaleObjectStateException sose = (StaleObjectStateException) e;
Serializable identifier = sose.getIdentifier();
if ( identifier != null ) {
Object entity = session.load( sose.getEntityName(), identifier );
if ( entity instanceof Serializable ) {
//avoid some user errors regarding boundary crossing
pe = new OptimisticLockException( null, e, entity );
return new OptimisticLockException( null, e, entity );
}
else {
pe = new OptimisticLockException( e );
return new OptimisticLockException( e );
}
}
else {
pe = new OptimisticLockException( e );
return new OptimisticLockException( e );
}
}
else {
pe = new OptimisticLockException( e );
return new OptimisticLockException( e );
}
return pe;
}

@SuppressWarnings( { "ThrowableInstanceNeverThrown", "unchecked" })
Expand Down Expand Up @@ -498,4 +493,5 @@ public <T> T unwrap(Class<T> type) {
//as I see this as an implementation detail that should not be exposed.
return query.unwrap( type );
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* JBoss, Home of Professional Open Source
* Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License, v. 2.1.
* This program is distributed in the hope that it will be useful, but WITHOUT A
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License,
* v.2.1 along with this distribution; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
package org.hibernate.search.jpa.impl;

import javax.persistence.OptimisticLockException;

import org.hibernate.HibernateException;
import org.hibernate.Session;

/**
* Helper class for FullTextQueryImpl to extract information out of an
* Hibernate OptimisticLockException and create the JPA counterpart.
*
* @author Sanne Grinovero <sanne@hibernate.org> (C) 2011 Red Hat Inc.
*/
public class OptimisticLockingCompatibilityHelper {

//This is the new classname, and is extended by the deprecated one so it is compatible with both types
private static final String parentOptimisticLockExceptionClassName = "org.hibernate.dialect.lock.OptimisticEntityLockException";
private static final String deprecatedOtimisticLockExceptionClassName = "org.hibernate.OptimisticLockException";
private static final Class optimisticLockExceptionClass = figureCompatibleOptimisticLockExceptionClass();

/**
* Looks for the new Hibernate class name, or fallbacks to the older one.
* @return the type of optimistic lock exceptions which Hibernate is going to throw
*/
private static Class figureCompatibleOptimisticLockExceptionClass() {
try {
return Class.forName( parentOptimisticLockExceptionClassName, true, Session.class.getClassLoader() );
}
catch ( ClassNotFoundException e ) {
// the failing class was introduced in Hibernate Core 4.0.0.CR7 only; fall back to old name when it's not found:
try {
return Class.forName( deprecatedOtimisticLockExceptionClassName, true, Session.class.getClassLoader() );
}
catch ( ClassNotFoundException e1 ) {
// this is fatal, will need to check for null at runtime
return null;
}
}
}

/**
* We might need different ways to extract the error message according to what is available at runtime
* @param e
* @return
*/
private static Object extractEntityOufOfException(HibernateException e) {
if ( parentOptimisticLockExceptionClassName.equals( optimisticLockExceptionClass.getName() ) ) {
org.hibernate.dialect.lock.OptimisticEntityLockException oele = (org.hibernate.dialect.lock.OptimisticEntityLockException) e;
return oele.getEntity();
}
else if ( deprecatedOtimisticLockExceptionClassName.equals( optimisticLockExceptionClass.getName() ) ) {
org.hibernate.OptimisticLockException oele = (org.hibernate.OptimisticLockException) e;
return oele.getEntity();
}
return null;
}

/**
* @param e an Hibernate runtime exception
* @return true if it's definitely an optimistic locking exception, false if we can't tell
*/
public static boolean isOptimisticLockException(HibernateException e) {
return optimisticLockExceptionClass != null && optimisticLockExceptionClass.isInstance( e );
}

/**
* Convert to a JPA exception; entity information is added only if possible
*/
public static OptimisticLockException convertToLockException(HibernateException e) {
Object entity = extractEntityOufOfException( e );
if ( entity != null) {
return new OptimisticLockException( e.getMessage(), e, entity );
}
else {
return new OptimisticLockException( e.getMessage(), e );
}
}

}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
<slf4jVersion>1.6.1</slf4jVersion>
<luceneVersion>3.4.0</luceneVersion>
<infinispanVersion>5.0.1.FINAL</infinispanVersion>
<hibernateVersion>4.0.0.CR6</hibernateVersion>
<hibernateVersion>4.0.0.CR7</hibernateVersion>
<hibernateCommonsAnnotationVersion>4.0.1.Final</hibernateCommonsAnnotationVersion>
<bytemanVersion>1.5.2</bytemanVersion>
<jbossLoggingVersion>3.1.0.CR2</jbossLoggingVersion>
Expand Down

0 comments on commit d03f928

Please sign in to comment.