Search in sources :

Example 31 with UncategorizedSQLException

use of org.springframework.jdbc.UncategorizedSQLException 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)

Example 32 with UncategorizedSQLException

use of org.springframework.jdbc.UncategorizedSQLException in project thinglinks by mqttsnet.

the class TdEngineController method insertData.

/**
 *@MethodDescription 插入数据
 *@param tableDto 插入数据需要的入参的实体类
 *@return R
 *@author thinglinks
 *@Date 2022/1/10 14:43
 */
@PostMapping("/insertData")
public R insertData(@Validated @RequestBody TableDto tableDto) {
    try {
        List<Fields> tagsFieldValues = tableDto.getTagsFieldValues();
        for (Fields fields : tagsFieldValues) {
            if (StringUtils.isBlank(fields.getFieldName()) || fields.getFieldValue() == null) {
                log.error("invalid operation: fieldName or fieldValue can not be empty");
                return R.fail("invalid operation: fieldName or fieldValue can not be empty");
            }
        }
        this.tdEngineService.insertData(tableDto);
        log.info("successful operation: insert data success");
        return R.ok();
    } catch (UncategorizedSQLException e) {
        String message = e.getCause().getMessage();
        try {
            message = message.substring(message.lastIndexOf("invalid operation"));
        } catch (Exception ex) {
        }
        log.error(message);
        return R.fail(message);
    }
}
Also used : UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) Fields(com.mqttsnet.thinglinks.tdengine.api.domain.Fields) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException)

Example 33 with UncategorizedSQLException

use of org.springframework.jdbc.UncategorizedSQLException in project springframework-source-5.1.x by wb02125055.

the class JdbcTemplateTests method testBatchUpdateWithBatchFailure.

@Test
public void testBatchUpdateWithBatchFailure() throws Exception {
    final String[] sql = { "A", "B", "C", "D" };
    given(this.statement.executeBatch()).willThrow(new BatchUpdateException(new int[] { 1, Statement.EXECUTE_FAILED, 1, Statement.EXECUTE_FAILED }));
    mockDatabaseMetaData(true);
    given(this.connection.createStatement()).willReturn(this.statement);
    JdbcTemplate template = new JdbcTemplate(this.dataSource, false);
    try {
        template.batchUpdate(sql);
    } catch (UncategorizedSQLException ex) {
        assertThat(ex.getSql(), equalTo("B; D"));
    }
}
Also used : UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) BatchUpdateException(java.sql.BatchUpdateException) Test(org.junit.Test)

Example 34 with UncategorizedSQLException

use of org.springframework.jdbc.UncategorizedSQLException in project springframework-source-5.1.x by wb02125055.

the class DataSourceTransactionManagerTests method testTransactionAwareDataSourceProxy.

@Test
public void testTransactionAwareDataSourceProxy() throws Exception {
    given(con.getAutoCommit()).willReturn(true);
    given(con.getWarnings()).willThrow(new SQLException());
    TransactionTemplate tt = new TransactionTemplate(tm);
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            // something transactional
            assertEquals(con, DataSourceUtils.getConnection(ds));
            TransactionAwareDataSourceProxy dsProxy = new TransactionAwareDataSourceProxy(ds);
            try {
                Connection tCon = dsProxy.getConnection();
                tCon.getWarnings();
                tCon.clearWarnings();
                assertEquals(con, ((ConnectionProxy) dsProxy.getConnection()).getTargetConnection());
                // should be ignored
                dsProxy.getConnection().close();
            } catch (SQLException ex) {
                throw new UncategorizedSQLException("", "", ex);
            }
        }
    });
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    InOrder ordered = inOrder(con);
    ordered.verify(con).setAutoCommit(false);
    ordered.verify(con).commit();
    ordered.verify(con).setAutoCommit(true);
    verify(con).close();
}
Also used : UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) InOrder(org.mockito.InOrder) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Connection(java.sql.Connection) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.Test)

Example 35 with UncategorizedSQLException

use of org.springframework.jdbc.UncategorizedSQLException in project springframework-source-5.1.x by wb02125055.

the class DataSourceTransactionManagerTests method testTransactionAwareDataSourceProxyWithSuspensionAndReobtaining.

@Test
public void testTransactionAwareDataSourceProxyWithSuspensionAndReobtaining() throws Exception {
    given(con.getAutoCommit()).willReturn(true);
    final TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRES_NEW);
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            // something transactional
            assertEquals(con, DataSourceUtils.getConnection(ds));
            final TransactionAwareDataSourceProxy dsProxy = new TransactionAwareDataSourceProxy(ds);
            dsProxy.setReobtainTransactionalConnections(true);
            try {
                assertEquals(con, ((ConnectionProxy) dsProxy.getConnection()).getTargetConnection());
                // should be ignored
                dsProxy.getConnection().close();
            } catch (SQLException ex) {
                throw new UncategorizedSQLException("", "", ex);
            }
            tt.execute(new TransactionCallbackWithoutResult() {

                @Override
                protected void doInTransactionWithoutResult(TransactionStatus status) {
                    // something transactional
                    assertEquals(con, DataSourceUtils.getConnection(ds));
                    try {
                        assertEquals(con, ((ConnectionProxy) dsProxy.getConnection()).getTargetConnection());
                        // should be ignored
                        dsProxy.getConnection().close();
                    } catch (SQLException ex) {
                        throw new UncategorizedSQLException("", "", ex);
                    }
                }
            });
            try {
                assertEquals(con, ((ConnectionProxy) dsProxy.getConnection()).getTargetConnection());
                // should be ignored
                dsProxy.getConnection().close();
            } catch (SQLException ex) {
                throw new UncategorizedSQLException("", "", ex);
            }
        }
    });
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    InOrder ordered = inOrder(con);
    ordered.verify(con).setAutoCommit(false);
    ordered.verify(con).commit();
    ordered.verify(con).setAutoCommit(true);
    verify(con, times(2)).close();
}
Also used : UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) InOrder(org.mockito.InOrder) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.Test)

Aggregations

UncategorizedSQLException (org.springframework.jdbc.UncategorizedSQLException)45 SQLException (java.sql.SQLException)34 InOrder (org.mockito.InOrder)25 TransactionStatus (org.springframework.transaction.TransactionStatus)25 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)25 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)25 Connection (java.sql.Connection)15 Test (org.junit.jupiter.api.Test)15 DataSource (javax.sql.DataSource)10 DataAccessException (org.springframework.dao.DataAccessException)9 Test (org.junit.Test)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 BatchUpdateException (java.sql.BatchUpdateException)4 Fields (com.mqttsnet.thinglinks.tdengine.api.domain.Fields)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 TransactionAwareDataSourceProxy (org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy)3 NonNull (org.springframework.lang.NonNull)3 FieldsVo (com.mqttsnet.thinglinks.tdengine.api.domain.FieldsVo)2 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)2 ObservationState (ca.nrc.cadc.caom2.ObservationState)1