Example usage for org.hibernate StaleObjectStateException getIdentifier

List of usage examples for org.hibernate StaleObjectStateException getIdentifier

Introduction

In this page you can find the example usage for org.hibernate StaleObjectStateException getIdentifier.

Prototype

public Serializable getIdentifier() 

Source Link

Usage

From source file:gr.interamerican.bo2.impl.open.hibernate.AbstractHibernatePersistenceUtility.java

License:Open Source License

/**
 * Logs a StaleObjectStateException.//from ww  w  .  java  2  s.  co m
 * 
 * @param he
 *        StaleObjectStateException. This object contains the class name
 *        and the id of the object on which the check failed.
 * @param o
 *        The object on which the worker operation is performed
 */
void logStaleObjectException(StaleObjectStateException he, Object o) {
    logHibernateException(he);
    specialLogHibernateException(he.getEntityName(), he.getIdentifier(), o);
}

From source file:org.granite.hibernate.HibernateOptimisticLockException.java

License:Open Source License

public static void rethrowOptimisticLockException(Session session, StaleObjectStateException sose) {
    Serializable identifier = sose.getIdentifier();
    if (identifier != null) {
        try {/*from w w w .  j a  va2  s. c  o  m*/
            Object entity = session.load(sose.getEntityName(), identifier);
            if (entity instanceof Serializable) {
                //avoid some user errors regarding boundary crossing
                throw new HibernateOptimisticLockException(null, sose, entity);
            }
        } catch (ObjectNotFoundException onfe) {
            // Ignored, StaleStateException will be rethrown
        }
    }
}

From source file:org.seamless.gwt.server.HibernateRemoteServiceServlet.java

License:Open Source License

@Override
protected String onBeforeResponseSerialized(Object result) {
    if (!getCurrentSession().getTransaction().isActive())
        return null;

    try {/*from  w w w  .  ja  v  a  2  s. c  o  m*/
        // Commit and cleanup
        log.fine("Committing the database transaction");
        getCurrentSession().getTransaction().commit();

    } catch (RuntimeException ex) {
        // Rollback only
        log.fine("Runtime exception occurred, considering transaction rollback: " + ex);
        try {
            if (getCurrentSession().getTransaction().isActive()) {
                log.fine("Trying to rollback database transaction after exception");
                getCurrentSession().getTransaction().rollback();
                log.fine("Transaction rolled back");
            }
        } catch (Throwable rbEx) {
            log.log(Level.SEVERE, "Could not rollback transaction after exception!", rbEx);
        }

        // Note that the exception you marshall to the client has to be
        // serializable/cross-compiled (in your 'shared'  or 'client' package)
        // and it has to implement com.google.gwt.user.client.rpc.IsSerializable
        // to pass the serialization policy security.

        if (ex instanceof StaleObjectStateException) {
            StaleObjectStateException sosEx = (StaleObjectStateException) ex;
            log.fine("Stale object state detected, serializing message to client for: " + sosEx);

            StringBuilder sb = new StringBuilder();
            sb.append("Concurrent modification error, ");
            sb.append("simultaneous modification of '").append(sosEx.getEntityName()).append("'");
            sb.append(" with identifier: ").append(sosEx.getIdentifier());

            ValidationException serializableException = new ValidationException(sb.toString());
            try {
                return encodeResponseForFailure(null, serializableException);
            } catch (SerializationException e) {
                log.fine("Can't serialize concurrent modification error message: " + e);
                throw ex;
            }

        }

        if (ex instanceof ConstraintViolationException) {
            ConstraintViolationException cvEx = (ConstraintViolationException) ex;
            log.fine("Integrity constraint violation detected, serializing message to client for: " + cvEx);

            StringBuilder sb = new StringBuilder();
            sb.append("Violation of database integrity, ");
            sb.append("error code: ").append(cvEx.getErrorCode());
            ValidationException serializableException = new ValidationException(sb.toString());
            try {
                return encodeResponseForFailure(null, serializableException);
            } catch (SerializationException e) {
                log.fine("Can't serialize integrity rule violation message: " + e);
                throw ex;
            }
        } else {
            throw ex;
        }
    }
    return null;
}

From source file:org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException.java

License:Apache License

public HibernateOptimisticLockingFailureException(StaleObjectStateException ex) {
    super(ex.getEntityName(), ex.getIdentifier(), ex);
}