use of org.springframework.dao.DataAccessResourceFailureException in project spring-framework by spring-projects.
the class TableMetaDataProviderFactory method createMetaDataProvider.
/**
* Create a TableMetaDataProvider based on the database metadata.
* @param dataSource used to retrieve metadata
* @param context the class that holds configuration and metadata
* @return instance of the TableMetaDataProvider implementation to be used
*/
public static TableMetaDataProvider createMetaDataProvider(DataSource dataSource, TableMetaDataContext context) {
try {
return (TableMetaDataProvider) JdbcUtils.extractDatabaseMetaData(dataSource, new DatabaseMetaDataCallback() {
@Override
public Object processMetaData(DatabaseMetaData databaseMetaData) throws SQLException {
String databaseProductName = JdbcUtils.commonDatabaseName(databaseMetaData.getDatabaseProductName());
boolean accessTableColumnMetaData = context.isAccessTableColumnMetaData();
TableMetaDataProvider provider;
if ("Oracle".equals(databaseProductName)) {
provider = new OracleTableMetaDataProvider(databaseMetaData, context.isOverrideIncludeSynonymsDefault());
} else if ("HSQL Database Engine".equals(databaseProductName)) {
provider = new HsqlTableMetaDataProvider(databaseMetaData);
} else if ("PostgreSQL".equals(databaseProductName)) {
provider = new PostgresTableMetaDataProvider(databaseMetaData);
} else if ("Apache Derby".equals(databaseProductName)) {
provider = new DerbyTableMetaDataProvider(databaseMetaData);
} else {
provider = new GenericTableMetaDataProvider(databaseMetaData);
}
if (logger.isDebugEnabled()) {
logger.debug("Using " + provider.getClass().getSimpleName());
}
provider.initializeWithMetaData(databaseMetaData);
if (accessTableColumnMetaData) {
provider.initializeWithTableColumnMetaData(databaseMetaData, context.getCatalogName(), context.getSchemaName(), context.getTableName());
}
return provider;
}
});
} catch (MetaDataAccessException ex) {
throw new DataAccessResourceFailureException("Error retrieving database metadata", ex);
}
}
use of org.springframework.dao.DataAccessResourceFailureException in project spring-framework by spring-projects.
the class TemporaryLobCreator method setClobAsAsciiStream.
@Override
public void setClobAsAsciiStream(PreparedStatement ps, int paramIndex, InputStream asciiStream, int contentLength) throws SQLException {
Clob clob = ps.getConnection().createClob();
try {
FileCopyUtils.copy(asciiStream, clob.setAsciiStream(1));
} catch (IOException ex) {
throw new DataAccessResourceFailureException("Could not copy into LOB stream", ex);
}
this.temporaryClobs.add(clob);
ps.setClob(paramIndex, clob);
if (logger.isDebugEnabled()) {
logger.debug(asciiStream != null ? "Copied ASCII stream into temporary CLOB with length " + contentLength : "Set CLOB to null");
}
}
use of org.springframework.dao.DataAccessResourceFailureException in project spring-framework by spring-projects.
the class TemporaryLobCreator method setClobAsCharacterStream.
@Override
public void setClobAsCharacterStream(PreparedStatement ps, int paramIndex, Reader characterStream, int contentLength) throws SQLException {
Clob clob = ps.getConnection().createClob();
try {
FileCopyUtils.copy(characterStream, clob.setCharacterStream(1));
} catch (IOException ex) {
throw new DataAccessResourceFailureException("Could not copy into LOB stream", ex);
}
this.temporaryClobs.add(clob);
ps.setClob(paramIndex, clob);
if (logger.isDebugEnabled()) {
logger.debug(characterStream != null ? "Copied character stream into temporary CLOB with length " + contentLength : "Set CLOB to null");
}
}
use of org.springframework.dao.DataAccessResourceFailureException in project spring-framework by spring-projects.
the class DataSourceTransactionManagerTests method doTestTransactionWithTimeout.
private void doTestTransactionWithTimeout(int timeout) throws Exception {
Assume.group(TestGroup.PERFORMANCE);
PreparedStatement ps = mock(PreparedStatement.class);
given(con.getAutoCommit()).willReturn(true);
given(con.prepareStatement("some SQL statement")).willReturn(ps);
TransactionTemplate tt = new TransactionTemplate(tm);
tt.setTimeout(timeout);
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
try {
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
try {
Thread.sleep(1500);
} catch (InterruptedException ex) {
}
try {
Connection con = DataSourceUtils.getConnection(ds);
PreparedStatement ps = con.prepareStatement("some SQL statement");
DataSourceUtils.applyTransactionTimeout(ps, ds);
} catch (SQLException ex) {
throw new DataAccessResourceFailureException("", ex);
}
}
});
if (timeout <= 1) {
fail("Should have thrown TransactionTimedOutException");
}
} catch (TransactionTimedOutException ex) {
if (timeout <= 1) {
// expected
} else {
throw ex;
}
}
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
if (timeout > 1) {
verify(ps).setQueryTimeout(timeout - 1);
verify(con).commit();
} else {
verify(con).rollback();
}
InOrder ordered = inOrder(con);
ordered.verify(con).setAutoCommit(false);
ordered.verify(con).setAutoCommit(true);
verify(con).close();
}
use of org.springframework.dao.DataAccessResourceFailureException in project spring-framework by spring-projects.
the class SQLErrorCodeSQLExceptionTranslatorTests method dataTruncationTranslation.
@Test
public void dataTruncationTranslation() {
SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES);
SQLException dataAccessEx = new SQLException("", "", 5);
DataTruncation dataTruncation = new DataTruncation(1, true, true, 1, 1, dataAccessEx);
DataAccessResourceFailureException daex = (DataAccessResourceFailureException) sext.translate("task", "SQL", dataTruncation);
assertEquals(dataTruncation, daex.getCause());
}
Aggregations