Search in sources :

Example 1 with SchemaChangeLock

use of org.motechproject.mds.domain.SchemaChangeLock in project motech by motech.

the class SchemaChangeLockManager method releaseLock.

@Transactional(propagation = Propagation.MANDATORY)
public void releaseLock(String comment) {
    LOGGER.info("Releasing lock for: {}", comment);
    final PersistenceManager pm = getPersistenceManager();
    final Boolean originalSerializedRead = pm.currentTransaction().getSerializeRead();
    try {
        pm.currentTransaction().setSerializeRead(true);
        SchemaChangeLock lock = getLock(pm);
        lock.setComment(null);
        lock.setTimestamp(null);
        LOGGER.info("Lock released for: {}", comment);
    } finally {
        pm.currentTransaction().setSerializeRead(originalSerializedRead);
    }
}
Also used : PersistenceManager(javax.jdo.PersistenceManager) SchemaChangeLock(org.motechproject.mds.domain.SchemaChangeLock) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with SchemaChangeLock

use of org.motechproject.mds.domain.SchemaChangeLock in project motech by motech.

the class SchemaChangeLockManager method getLock.

private SchemaChangeLock getLock(PersistenceManager pm) {
    Query query = pm.newQuery(SchemaChangeLock.class);
    query.setUnique(true);
    return (SchemaChangeLock) query.execute();
}
Also used : Query(javax.jdo.Query) SchemaChangeLock(org.motechproject.mds.domain.SchemaChangeLock)

Example 3 with SchemaChangeLock

use of org.motechproject.mds.domain.SchemaChangeLock in project motech by motech.

the class SchemaChangeLockManager method acquireLock.

@Transactional(propagation = Propagation.MANDATORY)
public void acquireLock(String comment) {
    LOGGER.info("Attempting to acquire lock for: {}", comment);
    final PersistenceManager pm = getPersistenceManager();
    final Boolean originalSerializedRead = pm.currentTransaction().getSerializeRead();
    try {
        pm.currentTransaction().setSerializeRead(true);
        final DateTime waitStartTime = DateUtil.now();
        SchemaChangeLock lock = null;
        while (lock == null && beforeTimeout(waitStartTime)) {
            // we keep retrying until timeout is reached
            try {
                LOGGER.info("Waiting for lock...");
                lock = getLock(pm);
            } catch (JDOException e) {
                LOGGER.debug("Exception thrown while waiting for lock, probably timed out", e);
            }
        }
        if (lock == null) {
            throw new IllegalStateException("Unable to acquire schema change lock for: " + comment);
        }
        lock.setComment(comment);
        lock.setTimestamp(DateUtil.now());
        LOGGER.info("Lock acquired for: {}", comment);
    } finally {
        pm.currentTransaction().setSerializeRead(originalSerializedRead);
    }
}
Also used : PersistenceManager(javax.jdo.PersistenceManager) SchemaChangeLock(org.motechproject.mds.domain.SchemaChangeLock) DateTime(org.joda.time.DateTime) JDOException(javax.jdo.JDOException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

SchemaChangeLock (org.motechproject.mds.domain.SchemaChangeLock)3 PersistenceManager (javax.jdo.PersistenceManager)2 Transactional (org.springframework.transaction.annotation.Transactional)2 JDOException (javax.jdo.JDOException)1 Query (javax.jdo.Query)1 DateTime (org.joda.time.DateTime)1