下面列出了怎么用org.hibernate.exception.GenericJDBCException的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void testFallbackExceptionTranslation() throws HibernateException {
SQLException sqlEx = new SQLException("argh", "27");
final GenericJDBCException gjex = new GenericJDBCException("mymsg", sqlEx);
try {
hibernateTemplate.execute(new HibernateCallback<Object>() {
@Override
public Object doInHibernate(org.hibernate.Session session) throws HibernateException {
throw gjex;
}
});
fail("Should have thrown DataIntegrityViolationException");
}
catch (DataIntegrityViolationException ex) {
// expected
assertEquals(sqlEx, ex.getCause());
assertTrue(ex.getMessage().indexOf("mymsg") != -1);
}
}
/**
* Returns true if the exception indicates that there is a problem connecting
* to the database as opposed to with the SQL.
*
* @param e
* @return
*/
private boolean shouldKeepTryingBecauseConnectionException(HibernateException e) {
// Need to know if it is a problem with the database not
// being accessible or if there is a problem with the SQL/data.
// If there is a problem accessibility of the database then
// want to keep trying writing the old data. But if it is
// a problem with the SQL/data then only want to try to write
// the good data from the batch a single time to make sure
// all good data is written.
// From javadocs for for org.hivernate.exception at
// http://docs.jboss.org/hibernate/orm/3.5/javadocs/org/hibernate/exception/package-frame.html
// can see that there are a couple of different exception types.
// From looking at documentation and testing found out that
// bad SQL is indicated by
// ConstraintViolationException
// DataException
// SQLGrammarException
// Appears that for bad connection could get:
// JDBCConnectionException (was not able to verify experimentally)
// GenericJDBCException (obtained when committing transaction with db turned off)
// So if exception is JDBCConnectionException or JDBCGenericException
// then should keep retrying until successful.
boolean keepTryingTillSuccessfull = e instanceof JDBCConnectionException ||
e instanceof GenericJDBCException;
return keepTryingTillSuccessfull;
}
@Override
public SQLExceptionConverter buildSQLExceptionConverter() {
return new SQLExceptionConverter() {
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
final int errorCode = sqlException.getErrorCode();
if (errorCode == SQLITE_CONSTRAINT) {
final String constraintName = EXTRACTER.extractConstraintName(sqlException);
return new ConstraintViolationException(message, sqlException, sql, constraintName);
} else if (errorCode == SQLITE_TOOBIG || errorCode == SQLITE_MISMATCH) {
return new DataException(message, sqlException, sql);
} else if (errorCode == SQLITE_BUSY || errorCode == SQLITE_LOCKED) {
return new LockAcquisitionException(message, sqlException, sql);
} else if ((errorCode >= SQLITE_IOERR && errorCode <= SQLITE_PROTOCOL) || errorCode == SQLITE_NOTADB) {
return new JDBCConnectionException(message, sqlException, sql);
}
return new GenericJDBCException(message, sqlException, sql);
}
};
}
@Override
protected int determineRowCount(int reportedRowCount, PreparedStatement statement) {
try {
return toCallableStatement( statement ).getInt( parameterPosition );
}
catch (SQLException sqle) {
sqlExceptionHelper.logExceptions( sqle, "could not extract row counts from CallableStatement" );
throw new GenericJDBCException( "could not extract row counts from CallableStatement", sqle );
}
}
/**
* Builds a minimal converter. The instance returned here just always converts to
* {@link org.hibernate.exception.GenericJDBCException}.
*
* @return The minimal converter.
*/
public static SQLExceptionConverter buildMinimalSQLExceptionConverter() {
return new SQLExceptionConverter() {
public JDBCException convert(SQLException sqlException, String message, String sql) {
return new GenericJDBCException( message, sqlException, sql );
}
};
}
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
for ( SQLExceptionConversionDelegate delegate : delegates ) {
final JDBCException jdbcException = delegate.convert( sqlException, message, sql );
if ( jdbcException != null ) {
return jdbcException;
}
}
return new GenericJDBCException( message, sqlException, sql );
}
@SuppressWarnings("ConstantConditions")
protected DataAccessException convertHibernateAccessException(HibernateException ex) {
if (ex instanceof JDBCException) {
return convertJdbcAccessException((JDBCException) ex, jdbcExceptionTranslator);
}
if (GenericJDBCException.class.equals(ex.getClass())) {
return convertJdbcAccessException((GenericJDBCException) ex, jdbcExceptionTranslator);
}
return SessionFactoryUtils.convertHibernateAccessException(ex);
}
protected int determineRowCount(int reportedRowCount, PreparedStatement statement) {
try {
return toCallableStatement( statement ).getInt( parameterPosition );
}
catch( SQLException sqle ) {
JDBCExceptionReporter.logExceptions( sqle, "could not extract row counts from CallableStatement" );
throw new GenericJDBCException( "could not extract row counts from CallableStatement", sqle );
}
}
@Override
public String getErrorDescription() {
if(getCause() != null && getCause() instanceof GenericJDBCException) {
String SQL = ((GenericJDBCException)getCause()).getSQL();
return String.format("A JPA error occurred (%s): <strong>%s</strong>. This is likely because the batch has broken some referential integrity. Check your cascade delete, in case of ...", getMessage(), getCause() == null ? "" : getCause().getMessage(), SQL);
}
return String.format("A JPA error occurred (%s): <strong>%s</strong>", getMessage(), getCause() == null ? "" : getCause().getMessage());
}
public List<String> getSource() {
List<String> sql = new ArrayList<String>();
if(getCause() != null && getCause() instanceof GenericJDBCException) {
sql.add(((GenericJDBCException)getCause()).getSQL());
}
return sql;
}
public void _save() {
if (!em().contains(this)) {
em().persist(this);
PlayPlugin.postEvent("JPASupport.objectPersisted", this);
}
avoidCascadeSaveLoops.set(new HashSet<JPABase>());
try {
saveAndCascade(true);
} finally {
avoidCascadeSaveLoops.get().clear();
}
try {
em().flush();
} catch (PersistenceException e) {
if (e.getCause() instanceof GenericJDBCException) {
throw new PersistenceException(((GenericJDBCException) e.getCause()).getSQL(), e);
} else {
throw e;
}
}
avoidCascadeSaveLoops.set(new HashSet<JPABase>());
try {
saveAndCascade(false);
} finally {
avoidCascadeSaveLoops.get().clear();
}
}
private Session getHibernateSession() {
if (session == null) {
try {
session = HibernateUtil.getSessionFactory().openSession();
} catch (GenericJDBCException e) {
//FIXME Show exception message and hide or disable all database based content
e.printStackTrace();
}
}
return session;
}
public JobLogView(ChipsterAdminUI app) {
this.app = app;
// do this before data source is attached to avoid one data update
setSizeFull();
table = new JobLogTable(this);
this.addComponent(getToolbar());
this.addComponent(table);
this.setExpandRatio(table, 1);
try {
dataSource = new JobLogContainer(this);
table.setContainerDataSource(dataSource);
table.setVisibleColumns(JobLogContainer.NATURAL_COL_ORDER);
table.setColumnHeaders(JobLogContainer.COL_HEADERS_ENGLISH);
table.setSortAscending(false);
table.setSortContainerPropertyId(JobLogContainer.END_TIME);
addFilter(JobLogContainer.END_TIME, DateContainerFilter.getToday());
} catch (GenericJDBCException e) {
logger.error("unable to read job database", e);
//FIXME Show exception message and hide or disable all database based content
return;
}
}
@Test(expected = GenericJDBCException.class)
public void testBatch() {
doInJDBC(this::batchInsert);
}
@Override
public boolean isSourceAvailable() {
return getCause() != null && getCause() instanceof GenericJDBCException;
}
/**
* Convert the given HibernateException to an appropriate exception
* from the {@code org.springframework.dao} hierarchy.
* <p>Will automatically apply a specified SQLExceptionTranslator to a
* Hibernate JDBCException, else rely on Hibernate's default translation.
* @param ex HibernateException that occured
* @return a corresponding DataAccessException
* @see SessionFactoryUtils#convertHibernateAccessException
* @see #setJdbcExceptionTranslator
*/
public DataAccessException convertHibernateAccessException(HibernateException ex) {
if (getJdbcExceptionTranslator() != null && ex instanceof JDBCException) {
return convertJdbcAccessException((JDBCException) ex, getJdbcExceptionTranslator());
}
else if (GenericJDBCException.class == ex.getClass()) {
return convertJdbcAccessException((GenericJDBCException) ex, getDefaultJdbcExceptionTranslator());
}
return SessionFactoryUtils.convertHibernateAccessException(ex);
}
/**
* Convert the given HibernateException to an appropriate exception
* from the {@code org.springframework.dao} hierarchy.
* <p>Will automatically apply a specified SQLExceptionTranslator to a
* Hibernate JDBCException, else rely on Hibernate's default translation.
* @param ex HibernateException that occurred
* @return a corresponding DataAccessException
* @see SessionFactoryUtils#convertHibernateAccessException
* @see #setJdbcExceptionTranslator
*/
protected DataAccessException convertHibernateAccessException(HibernateException ex) {
if (getJdbcExceptionTranslator() != null && ex instanceof JDBCException) {
return convertJdbcAccessException((JDBCException) ex, getJdbcExceptionTranslator());
}
else if (GenericJDBCException.class == ex.getClass()) {
return convertJdbcAccessException((GenericJDBCException) ex, getDefaultJdbcExceptionTranslator());
}
return SessionFactoryUtils.convertHibernateAccessException(ex);
}
/**
* Convert the given HibernateException to an appropriate exception
* from the {@code org.springframework.dao} hierarchy.
* <p>Will automatically apply a specified SQLExceptionTranslator to a
* Hibernate JDBCException, else rely on Hibernate's default translation.
* @param ex HibernateException that occured
* @return a corresponding DataAccessException
* @see SessionFactoryUtils#convertHibernateAccessException
* @see #setJdbcExceptionTranslator
*/
public DataAccessException convertHibernateAccessException(HibernateException ex) {
if (getJdbcExceptionTranslator() != null && ex instanceof JDBCException) {
return convertJdbcAccessException((JDBCException) ex, getJdbcExceptionTranslator());
}
else if (GenericJDBCException.class == ex.getClass()) {
return convertJdbcAccessException((GenericJDBCException) ex, getDefaultJdbcExceptionTranslator());
}
return SessionFactoryUtils.convertHibernateAccessException(ex);
}
/**
* Convert the given HibernateException to an appropriate exception
* from the {@code org.springframework.dao} hierarchy.
* <p>Will automatically apply a specified SQLExceptionTranslator to a
* Hibernate JDBCException, else rely on Hibernate's default translation.
* @param ex HibernateException that occurred
* @return a corresponding DataAccessException
* @see SessionFactoryUtils#convertHibernateAccessException
* @see #setJdbcExceptionTranslator
*/
protected DataAccessException convertHibernateAccessException(HibernateException ex) {
if (getJdbcExceptionTranslator() != null && ex instanceof JDBCException) {
return convertJdbcAccessException((JDBCException) ex, getJdbcExceptionTranslator());
}
else if (GenericJDBCException.class == ex.getClass()) {
return convertJdbcAccessException((GenericJDBCException) ex, getDefaultJdbcExceptionTranslator());
}
return SessionFactoryUtils.convertHibernateAccessException(ex);
}