Search in sources :

Example 1 with DBRuntimeException

use of org.olat.core.logging.DBRuntimeException in project OpenOLAT by OpenOLAT.

the class DBImpl method updateObject.

/**
 * Update an object.
 *
 * @param object
 */
@Override
public void updateObject(Object object) {
    EntityManager em = getCurrentEntityManager();
    EntityTransaction trx = em.getTransaction();
    if (unusableTrx(trx)) {
        // some program bug
        throw new DBRuntimeException("cannot update in a transaction that is rolledback or committed " + object);
    }
    try {
        getSession(em).update(object);
    } catch (HibernateException e) {
        // we have some error
        trx.setRollbackOnly();
        getData().setError(e);
        throw new DBRuntimeException("Update object failed in transaction. Query: " + object, e);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) DBRuntimeException(org.olat.core.logging.DBRuntimeException) HibernateException(org.hibernate.HibernateException)

Example 2 with DBRuntimeException

use of org.olat.core.logging.DBRuntimeException in project OpenOLAT by OpenOLAT.

the class DBImpl method deleteObject.

/**
 * Delete an object.
 *
 * @param object
 */
@Override
public void deleteObject(Object object) {
    EntityManager em = getCurrentEntityManager();
    EntityTransaction trx = em.getTransaction();
    if (unusableTrx(trx)) {
        // some program bug
        throw new DBRuntimeException("cannot delete in a transaction that is rolledback or committed " + object);
    }
    try {
        Object relaoded = em.merge(object);
        em.remove(relaoded);
        if (log.isDebug()) {
            log.debug("delete (trans " + trx.hashCode() + ") class " + object.getClass().getName() + " = " + object.toString());
        }
    } catch (HibernateException e) {
        // we have some error
        trx.setRollbackOnly();
        getData().setError(e);
        throw new DBRuntimeException("Delete of object failed: " + object, e);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) DBRuntimeException(org.olat.core.logging.DBRuntimeException) HibernateException(org.hibernate.HibernateException)

Example 3 with DBRuntimeException

use of org.olat.core.logging.DBRuntimeException in project OpenOLAT by OpenOLAT.

the class WebDAVAuthManager method updateWebDAVPassword.

private void updateWebDAVPassword(Identity doer, Identity identity, String authUsername, String password, String provider, List<Authentication> authentications) {
    Authentication authentication = getAndRemoveAuthentication(provider, authentications);
    if (authentication == null) {
        // create new authentication for provider OLAT
        try {
            dbInstance.commit();
            Identity reloadedIdentity = securityManager.loadIdentityByKey(identity.getKey());
            securityManager.createAndPersistAuthentication(reloadedIdentity, provider, authUsername, password, loginModule.getDefaultHashAlgorithm());
            log.audit(doer.getName() + " created new WebDAV authentication for identity: " + identity.getKey() + " (" + authUsername + ")");
        } catch (DBRuntimeException e) {
            log.error("Cannot create webdav password with provider " + provider + " for identity:" + identity, e);
            dbInstance.commit();
        }
    } else {
        try {
            dbInstance.commit();
            securityManager.updateCredentials(authentication, password, loginModule.getDefaultHashAlgorithm());
            log.audit(doer.getName() + " set new WebDAV password for identity: " + identity.getKey() + " (" + authUsername + ")");
        } catch (Exception e) {
            log.error("Cannot update webdav password with provider " + provider + " for identity:" + identity, e);
            dbInstance.commit();
        }
    }
}
Also used : DBRuntimeException(org.olat.core.logging.DBRuntimeException) Authentication(org.olat.basesecurity.Authentication) Identity(org.olat.core.id.Identity) AssertException(org.olat.core.logging.AssertException) DBRuntimeException(org.olat.core.logging.DBRuntimeException)

Example 4 with DBRuntimeException

use of org.olat.core.logging.DBRuntimeException in project OpenOLAT by OpenOLAT.

the class ClusterLocker method event.

/**
 * receives all sign on / sign off events so it can release locks of users
 * which have or are logged off
 *
 * @see org.olat.core.util.event.GenericEventListener#event(org.olat.core.gui.control.Event)
 */
@Override
public void event(Event event) {
    SignOnOffEvent se = (SignOnOffEvent) event;
    if (!se.isSignOn() && se.isEventOnThisNode()) {
        // it is a "logout" event - we are only interested in logout events
        // and it is from our VM => only release all locks from within one VM
        Long identKey = se.getIdentityKey();
        // since the lock is reentrant, a lock could be freed while a session still is in a locked workflow (2x lock and then once freed)
        try {
            clusterLockManager.releaseAllLocksFor(identKey);
            DBFactory.getInstance().commit();
        } catch (DBRuntimeException dbEx) {
            log.warn("releaseAllLocksFor failed, close session and try it again for identName=" + identKey);
            // TODO: 2010-04-23 Transactions [eglis]: OLAT-4318: this rollback has possibly unwanted
            // side effects, as it rolls back any changes with this transaction during this
            // event handling. Nicer would be to be done in the outmost-possible place, e.g. dofire()
            DBFactory.getInstance().rollbackAndCloseSession();
            // try again with new db-session
            log.info("try again to release all locks for identName=" + identKey);
            clusterLockManager.releaseAllLocksFor(identKey);
            log.info("Done, released all locks for identName=" + identKey);
        }
    }
}
Also used : DBRuntimeException(org.olat.core.logging.DBRuntimeException) SignOnOffEvent(org.olat.core.util.SignOnOffEvent)

Example 5 with DBRuntimeException

use of org.olat.core.logging.DBRuntimeException in project OpenOLAT by OpenOLAT.

the class LockTest method testSaveEvent.

@Test
public void testSaveEvent() {
    Identity identity = JunitTestHelper.createAndPersistIdentityAsRndUser("lock-save-event-");
    dbInstance.closeSession();
    log.info("Created identity=" + identity);
    // The group has no creation date -> commit will fail
    GroupImpl entry = new GroupImpl();
    entry.setName("bar");
    try {
        dbInstance.saveObject(entry);
        dbInstance.commit();
        fail("Should generate an error");
    } catch (DBRuntimeException dre) {
        log.info("DB connection is in error-state");
    }
    // DB transaction must be in error state for this test
    try {
        Locker locker = clusterCoordinator.getLocker();
        assertTrue(locker instanceof ClusterLocker);
        log.info("ClusterLocker created");
        Event event = new SignOnOffEvent(identity, false);
        log.info("START locker.event(event)");
        ((ClusterLocker) locker).event(event);
        log.info("DONE locker.event(event)");
    } catch (Exception ex) {
        log.error("", ex);
        fail("BLOCKER : ClusterLocker.event is not error-safe, db exception could happen and de-register event-listener");
    }
}
Also used : Locker(org.olat.core.util.coordinate.Locker) DBRuntimeException(org.olat.core.logging.DBRuntimeException) GroupImpl(org.olat.basesecurity.model.GroupImpl) SignOnOffEvent(org.olat.core.util.SignOnOffEvent) SignOnOffEvent(org.olat.core.util.SignOnOffEvent) Event(org.olat.core.gui.control.Event) Identity(org.olat.core.id.Identity) DBRuntimeException(org.olat.core.logging.DBRuntimeException) Test(org.junit.Test)

Aggregations

DBRuntimeException (org.olat.core.logging.DBRuntimeException)24 HibernateException (org.hibernate.HibernateException)10 EntityManager (javax.persistence.EntityManager)8 EntityTransaction (javax.persistence.EntityTransaction)8 Identity (org.olat.core.id.Identity)8 AssertException (org.olat.core.logging.AssertException)6 SQLException (java.sql.SQLException)4 RollbackException (javax.persistence.RollbackException)4 Test (org.junit.Test)4 Authentication (org.olat.basesecurity.Authentication)4 GroupImpl (org.olat.basesecurity.model.GroupImpl)4 KnownIssueException (org.olat.core.logging.KnownIssueException)4 SignOnOffEvent (org.olat.core.util.SignOnOffEvent)4 Date (java.util.Date)2 ObjectNotFoundException (org.hibernate.ObjectNotFoundException)2 Query (org.hibernate.Query)2 StaleObjectStateException (org.hibernate.StaleObjectStateException)2 CollaborationTools (org.olat.collaboration.CollaborationTools)2 Event (org.olat.core.gui.control.Event)2 Locker (org.olat.core.util.coordinate.Locker)2