类org.hibernate.exception.GenericJDBCException源码实例Demo

下面列出了怎么用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);
	}
}
 
源代码2 项目: core   文件: DataDbLogger.java
/**
 * 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;
}
 
源代码3 项目: yeti   文件: SQLiteDialect.java
@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);
        }
    };
}
 
源代码4 项目: lams   文件: Expectations.java
@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 );
	}
}
 
源代码5 项目: lams   文件: SQLExceptionConverterFactory.java
/**
 * 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 );
		}
	};
}
 
源代码6 项目: lams   文件: StandardSQLExceptionConverter.java
@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 );
}
 
源代码7 项目: gorm-hibernate5   文件: GrailsHibernateTemplate.java
@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);
}
 
源代码8 项目: cacheonix-core   文件: Expectations.java
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 );
	}
}
 
源代码9 项目: restcommander   文件: JPAException.java
@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());
}
 
源代码10 项目: restcommander   文件: JPAException.java
public List<String> getSource() {
    List<String> sql = new ArrayList<String>();
    if(getCause() != null && getCause() instanceof GenericJDBCException) {
        sql.add(((GenericJDBCException)getCause()).getSQL());
    }
    return sql;
}
 
源代码11 项目: restcommander   文件: JPABase.java
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();
    }
}
 
源代码12 项目: chipster   文件: StatView.java
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;
}
 
源代码13 项目: chipster   文件: JobLogView.java
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);
}
 
源代码15 项目: restcommander   文件: JPAException.java
@Override
public boolean isSourceAvailable() {
    return getCause() != null && getCause() instanceof GenericJDBCException;
}
 
源代码16 项目: lams   文件: HibernateAccessor.java
/**
 * 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);
}
 
源代码17 项目: lams   文件: HibernateTransactionManager.java
/**
 * 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);
}
 
源代码18 项目: spring4-understanding   文件: HibernateAccessor.java
/**
 * 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);
}
 
 类所在包
 类方法
 同包方法