Search in sources :

Example 1 with ReadOnlyServerException

use of org.alfresco.service.transaction.ReadOnlyServerException in project alfresco-repository by Alfresco.

the class TransactionServiceImplTest method testReadOnlyTxn.

@Test
public void testReadOnlyTxn() throws Exception {
    // start a read-only transaction
    transactionService.setAllowWrite(false, vetoName);
    UserTransaction txn = transactionService.getUserTransaction();
    txn.begin();
    // do some writing
    try {
        nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, getName() + "_" + System.currentTimeMillis());
        txn.commit();
        fail("Read-only transaction wasn't detected");
    } catch (ReadOnlyServerException e) {
        // This is now thrown at the lower layers, but it *is* possible for one of the later
        // exceptions to get through: Fixed ALF-3884: Share does not report access denied exceptions correctly
        @SuppressWarnings("unused") int i = 0;
    } catch (InvalidDataAccessApiUsageException e) {
        // expected this ...
        @SuppressWarnings("unused") int i = 0;
    } catch (TransientDataAccessResourceException e) {
        // or this - for MySQL (java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed.)
        @SuppressWarnings("unused") int i = 0;
    } catch (IllegalStateException e) {
        // or this - for MS SQLServer, Oracle (via AbstractNodeDAOImpl.getCurrentTransaction)
        @SuppressWarnings("unused") int i = 0;
    } catch (UncategorizedSQLException e) {
        // or this - for PostgreSQL (org.postgresql.util.PSQLException: ERROR: transaction is read-only)
        if (dialect instanceof PostgreSQLDialect) {
            // ALF-4226
            @SuppressWarnings("unused") int i = 0;
        } else {
            throw e;
        }
    } finally {
        transactionService.setAllowWrite(true, vetoName);
        try {
            txn.rollback();
        } catch (Throwable e) {
        }
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) PostgreSQLDialect(org.alfresco.repo.domain.dialect.PostgreSQLDialect) TransientDataAccessResourceException(org.springframework.dao.TransientDataAccessResourceException) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) ReadOnlyServerException(org.alfresco.service.transaction.ReadOnlyServerException) BaseSpringTest(org.alfresco.util.BaseSpringTest) Test(org.junit.Test)

Aggregations

UserTransaction (javax.transaction.UserTransaction)1 PostgreSQLDialect (org.alfresco.repo.domain.dialect.PostgreSQLDialect)1 ReadOnlyServerException (org.alfresco.service.transaction.ReadOnlyServerException)1 BaseSpringTest (org.alfresco.util.BaseSpringTest)1 Test (org.junit.Test)1 InvalidDataAccessApiUsageException (org.springframework.dao.InvalidDataAccessApiUsageException)1 TransientDataAccessResourceException (org.springframework.dao.TransientDataAccessResourceException)1 UncategorizedSQLException (org.springframework.jdbc.UncategorizedSQLException)1