org.hibernate.exception.GenericJDBCException Java Examples

The following examples show how to use org.hibernate.exception.GenericJDBCException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SQLiteDialect.java    From yeti with MIT License 6 votes vote down vote up
@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);
        }
    };
}
 
Example #2
Source File: HibernateTemplateTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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);
	}
}
 
Example #3
Source File: DataDbLogger.java    From core with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #4
Source File: Expectations.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
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 );
	}
}
 
Example #5
Source File: JobLogView.java    From chipster with MIT License 5 votes vote down vote up
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;
		}		
	}
 
Example #6
Source File: StatView.java    From chipster with MIT License 5 votes vote down vote up
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;
}
 
Example #7
Source File: JPABase.java    From restcommander with Apache License 2.0 5 votes vote down vote up
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();
    }
}
 
Example #8
Source File: JPAException.java    From restcommander with Apache License 2.0 5 votes vote down vote up
public List<String> getSource() {
    List<String> sql = new ArrayList<String>();
    if(getCause() != null && getCause() instanceof GenericJDBCException) {
        sql.add(((GenericJDBCException)getCause()).getSQL());
    }
    return sql;
}
 
Example #9
Source File: JPAException.java    From restcommander with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #10
Source File: GrailsHibernateTemplate.java    From gorm-hibernate5 with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #11
Source File: StandardSQLExceptionConverter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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 );
}
 
Example #12
Source File: SQLExceptionConverterFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 );
		}
	};
}
 
Example #13
Source File: Expectations.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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 );
	}
}
 
Example #14
Source File: SQLServerGeneratedKeysBatchPreparedStatementTest.java    From high-performance-java-persistence with Apache License 2.0 4 votes vote down vote up
@Test(expected = GenericJDBCException.class)
public void testBatch() {
    doInJDBC(this::batchInsert);
}
 
Example #15
Source File: JPAException.java    From restcommander with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isSourceAvailable() {
    return getCause() != null && getCause() instanceof GenericJDBCException;
}
 
Example #16
Source File: HibernateAccessor.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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);
}
 
Example #17
Source File: HibernateTransactionManager.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * 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);
}
 
Example #18
Source File: HibernateAccessor.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * 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);
}
 
Example #19
Source File: HibernateTransactionManager.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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);
}