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) {
}
}
}
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);
}
}
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"));
}
}
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();
}
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();
}
Aggregations