Java Code Examples for org.hibernate.exception.GenericJDBCException

The following examples show how to use org.hibernate.exception.GenericJDBCException. These examples are extracted from open source projects. 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
@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 2
Source Project: core   Source File: DataDbLogger.java    License: 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 3
Source Project: yeti   Source File: SQLiteDialect.java    License: 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 4
Source Project: lams   Source File: Expectations.java    License: 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 5
/**
 * 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 6
@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 7
Source Project: gorm-hibernate5   Source File: GrailsHibernateTemplate.java    License: 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 8
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 9
Source Project: restcommander   Source File: JPAException.java    License: 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 Project: restcommander   Source File: JPAException.java    License: 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 11
Source Project: restcommander   Source File: JPABase.java    License: 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 12
Source Project: chipster   Source File: StatView.java    License: 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 13
Source Project: chipster   Source File: JobLogView.java    License: 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 14
@Test(expected = GenericJDBCException.class)
public void testBatch() {
    doInJDBC(this::batchInsert);
}
 
Example 15
Source Project: restcommander   Source File: JPAException.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public boolean isSourceAvailable() {
    return getCause() != null && getCause() instanceof GenericJDBCException;
}
 
Example 16
Source Project: lams   Source File: HibernateAccessor.java    License: 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
/**
 * 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 Project: spring4-understanding   Source File: HibernateAccessor.java    License: 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
/**
 * 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);
}