List of usage examples for org.springframework.dao DataAccessResourceFailureException DataAccessResourceFailureException
public DataAccessResourceFailureException(String msg, @Nullable Throwable cause)
From source file:org.springframework.orm.hibernate.SessionFactoryUtils.java
/** * Get a new Hibernate Session from the given SessionFactory. * Will return a new Session even if there already is a pre-bound * Session for the given SessionFactory. * <p>Within a transaction, this method will create a new Session * that shares the transaction's JDBC Connection. More specifically, * it will use the same JDBC Connection as the pre-bound Hibernate Session. * @param sessionFactory Hibernate SessionFactory to create the session with * @param entityInterceptor Hibernate entity interceptor, or <code>null</code> if none * @return the new Session//w ww. j a v a2 s . c om */ public static Session getNewSession(SessionFactory sessionFactory, Interceptor entityInterceptor) { Assert.notNull(sessionFactory, "No SessionFactory specified"); try { SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager .getResource(sessionFactory); if (sessionHolder != null && !sessionHolder.isEmpty()) { if (entityInterceptor != null) { return sessionFactory.openSession(sessionHolder.getAnySession().connection(), entityInterceptor); } else { return sessionFactory.openSession(sessionHolder.getAnySession().connection()); } } else { if (entityInterceptor != null) { return sessionFactory.openSession(entityInterceptor); } else { return sessionFactory.openSession(); } } } catch (HibernateException ex) { throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex); } }
From source file:org.springframework.orm.hibernate.support.AbstractLobType.java
/** * This implementation delegates to nullSafeSetInternal, * passing in a transaction-synchronized LobCreator for the * LobHandler of this type.//from w ww. j a va 2 s . c o m * @see #nullSafeSetInternal */ public final void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException { if (this.lobHandler == null) { throw new IllegalStateException("No LobHandler found for configuration - " + "lobHandler property must be set on LocalSessionFactoryBean"); } LobCreator lobCreator = this.lobHandler.getLobCreator(); try { nullSafeSetInternal(st, index, value, lobCreator); } catch (IOException ex) { throw new HibernateException("I/O errors during LOB access", ex); } if (TransactionSynchronizationManager.isSynchronizationActive()) { logger.debug("Registering Spring transaction synchronization for Hibernate LOB type"); TransactionSynchronizationManager .registerSynchronization(new SpringLobCreatorSynchronization(lobCreator)); } else { if (this.jtaTransactionManager != null) { try { int jtaStatus = this.jtaTransactionManager.getStatus(); if (jtaStatus == Status.STATUS_ACTIVE || jtaStatus == Status.STATUS_MARKED_ROLLBACK) { logger.debug("Registering JTA transaction synchronization for Hibernate LOB type"); this.jtaTransactionManager.getTransaction() .registerSynchronization(new JtaLobCreatorSynchronization(lobCreator)); return; } } catch (Exception ex) { throw new DataAccessResourceFailureException( "Could not register synchronization with JTA TransactionManager", ex); } } throw new IllegalStateException("Active Spring transaction synchronization or active " + "JTA transaction with 'jtaTransactionManager' on LocalSessionFactoryBean required"); } }
From source file:org.springframework.orm.hibernate3.SessionFactoryUtils.java
/** * Get a Hibernate Session for the given SessionFactory. Is aware of and will * return any existing corresponding Session bound to the current thread, for * example when using {@link HibernateTransactionManager}. Will create a new * Session otherwise, if "allowCreate" is {@code true}. * <p>This is the {@code getSession} method used by typical data access code, * in combination with {@code releaseSession} called when done with * the Session. Note that HibernateTemplate allows to write data access code * without caring about such resource handling. * @param sessionFactory Hibernate SessionFactory to create the session with * @param allowCreate whether a non-transactional Session should be created * when no transactional Session can be found for the current thread * @return the Hibernate Session//w ww .j a va 2 s . c om * @throws DataAccessResourceFailureException if the Session couldn't be created * @throws IllegalStateException if no thread-bound Session found and * "allowCreate" is {@code false} * @see #getSession(SessionFactory, Interceptor, SQLExceptionTranslator) * @see #releaseSession * @see HibernateTemplate */ public static Session getSession(SessionFactory sessionFactory, boolean allowCreate) throws DataAccessResourceFailureException, IllegalStateException { try { return doGetSession(sessionFactory, null, null, allowCreate); } catch (HibernateException ex) { throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex); } }
From source file:org.springframework.orm.hibernate3.SessionFactoryUtils.java
/** * Get a Hibernate Session for the given SessionFactory. Is aware of and will * return any existing corresponding Session bound to the current thread, for * example when using {@link HibernateTransactionManager}. Will always create * a new Session otherwise./* w w w . j a v a2s. co m*/ * <p>Supports setting a Session-level Hibernate entity interceptor that allows * to inspect and change property values before writing to and reading from the * database. Such an interceptor can also be set at the SessionFactory level * (i.e. on LocalSessionFactoryBean), on HibernateTransactionManager, etc. * @param sessionFactory Hibernate SessionFactory to create the session with * @param entityInterceptor Hibernate entity interceptor, or {@code null} if none * @param jdbcExceptionTranslator SQLExcepionTranslator to use for flushing the * Session on transaction synchronization (may be {@code null}; only used * when actually registering a transaction synchronization) * @return the Hibernate Session * @throws DataAccessResourceFailureException if the Session couldn't be created * @see LocalSessionFactoryBean#setEntityInterceptor * @see HibernateTemplate#setEntityInterceptor */ public static Session getSession(SessionFactory sessionFactory, Interceptor entityInterceptor, SQLExceptionTranslator jdbcExceptionTranslator) throws DataAccessResourceFailureException { try { return doGetSession(sessionFactory, entityInterceptor, jdbcExceptionTranslator, true); } catch (HibernateException ex) { throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex); } }
From source file:org.springframework.orm.hibernate3.SessionFactoryUtils.java
/** * Retrieve a Session from the given SessionHolder, potentially from a * JTA transaction synchronization./* ww w . j a v a2s. co m*/ * @param sessionHolder the SessionHolder to check * @param sessionFactory the SessionFactory to get the JTA TransactionManager from * @param jdbcExceptionTranslator SQLExcepionTranslator to use for flushing the * Session on transaction synchronization (may be {@code null}) * @return the associated Session, if any * @throws DataAccessResourceFailureException if the Session couldn't be created */ private static Session getJtaSynchronizedSession(SessionHolder sessionHolder, SessionFactory sessionFactory, SQLExceptionTranslator jdbcExceptionTranslator) throws DataAccessResourceFailureException { // JTA synchronization is only possible with a javax.transaction.TransactionManager. // We'll check the Hibernate SessionFactory: If a TransactionManagerLookup is specified // in Hibernate configuration, it will contain a TransactionManager reference. TransactionManager jtaTm = getJtaTransactionManager(sessionFactory, sessionHolder.getAnySession()); if (jtaTm != null) { // Check whether JTA transaction management is active -> // fetch pre-bound Session for the current JTA transaction, if any. // (just necessary for JTA transaction suspension, with an individual // Hibernate Session per currently active/suspended transaction) try { // Look for transaction-specific Session. Transaction jtaTx = jtaTm.getTransaction(); if (jtaTx != null) { int jtaStatus = jtaTx.getStatus(); if (jtaStatus == Status.STATUS_ACTIVE || jtaStatus == Status.STATUS_MARKED_ROLLBACK) { Session session = sessionHolder.getValidatedSession(jtaTx); if (session == null && !sessionHolder.isSynchronizedWithTransaction()) { // No transaction-specific Session found: If not already marked as // synchronized with transaction, register the default thread-bound // Session as JTA-transactional. If there is no default Session, // we're a new inner JTA transaction with an outer one being suspended: // In that case, we'll return null to trigger opening of a new Session. session = sessionHolder.getValidatedSession(); if (session != null) { logger.debug( "Registering JTA transaction synchronization for existing Hibernate Session"); sessionHolder.addSession(jtaTx, session); jtaTx.registerSynchronization(new SpringJtaSynchronizationAdapter( new SpringSessionSynchronization(sessionHolder, sessionFactory, jdbcExceptionTranslator, false), jtaTm)); sessionHolder.setSynchronizedWithTransaction(true); // Switch to FlushMode.AUTO, as we have to assume a thread-bound Session // with FlushMode.NEVER, which needs to allow flushing within the transaction. FlushMode flushMode = session.getFlushMode(); if (flushMode.lessThan(FlushMode.COMMIT)) { session.setFlushMode(FlushMode.AUTO); sessionHolder.setPreviousFlushMode(flushMode); } } } return session; } } // No transaction active -> simply return default thread-bound Session, if any // (possibly from OpenSessionInViewFilter/Interceptor). return sessionHolder.getValidatedSession(); } catch (Throwable ex) { throw new DataAccessResourceFailureException("Could not check JTA transaction", ex); } } else { // No JTA TransactionManager -> simply return default thread-bound Session, if any // (possibly from OpenSessionInViewFilter/Interceptor). return sessionHolder.getValidatedSession(); } }
From source file:org.springframework.orm.hibernate3.SessionFactoryUtils.java
/** * Register a JTA synchronization for the given Session, if any. * @param sessionHolder the existing thread-bound SessionHolder, if any * @param session the Session to register * @param sessionFactory the SessionFactory that the Session was created with * @param jdbcExceptionTranslator SQLExcepionTranslator to use for flushing the * Session on transaction synchronization (may be {@code null}) *///from www. ja v a 2 s . c o m private static void registerJtaSynchronization(Session session, SessionFactory sessionFactory, SQLExceptionTranslator jdbcExceptionTranslator, SessionHolder sessionHolder) { // JTA synchronization is only possible with a javax.transaction.TransactionManager. // We'll check the Hibernate SessionFactory: If a TransactionManagerLookup is specified // in Hibernate configuration, it will contain a TransactionManager reference. TransactionManager jtaTm = getJtaTransactionManager(sessionFactory, session); if (jtaTm != null) { try { Transaction jtaTx = jtaTm.getTransaction(); if (jtaTx != null) { int jtaStatus = jtaTx.getStatus(); if (jtaStatus == Status.STATUS_ACTIVE || jtaStatus == Status.STATUS_MARKED_ROLLBACK) { logger.debug("Registering JTA transaction synchronization for new Hibernate Session"); SessionHolder holderToUse = sessionHolder; // Register JTA Transaction with existing SessionHolder. // Create a new SessionHolder if none existed before. if (holderToUse == null) { holderToUse = new SessionHolder(jtaTx, session); } else { holderToUse.addSession(jtaTx, session); } jtaTx.registerSynchronization( new SpringJtaSynchronizationAdapter(new SpringSessionSynchronization(holderToUse, sessionFactory, jdbcExceptionTranslator, true), jtaTm)); holderToUse.setSynchronizedWithTransaction(true); if (holderToUse != sessionHolder) { TransactionSynchronizationManager.bindResource(sessionFactory, holderToUse); } } } } catch (Throwable ex) { throw new DataAccessResourceFailureException( "Could not register synchronization with JTA TransactionManager", ex); } } }
From source file:org.springframework.orm.hibernate3.SessionFactoryUtils.java
/** * Get a new Hibernate Session from the given SessionFactory. * Will return a new Session even if there already is a pre-bound * Session for the given SessionFactory. * <p>Within a transaction, this method will create a new Session * that shares the transaction's JDBC Connection. More specifically, * it will use the same JDBC Connection as the pre-bound Hibernate Session. * @param sessionFactory Hibernate SessionFactory to create the session with * @param entityInterceptor Hibernate entity interceptor, or {@code null} if none * @return the new Session/* w w w.j a va 2 s .c om*/ */ @SuppressWarnings("deprecation") public static Session getNewSession(SessionFactory sessionFactory, Interceptor entityInterceptor) { Assert.notNull(sessionFactory, "No SessionFactory specified"); try { SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager .getResource(sessionFactory); if (sessionHolder != null && !sessionHolder.isEmpty()) { if (entityInterceptor != null) { return sessionFactory.openSession(sessionHolder.getAnySession().connection(), entityInterceptor); } else { return sessionFactory.openSession(sessionHolder.getAnySession().connection()); } } else { if (entityInterceptor != null) { return sessionFactory.openSession(entityInterceptor); } else { return sessionFactory.openSession(); } } } catch (HibernateException ex) { throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex); } }
From source file:org.springframework.orm.hibernate3.SessionFactoryUtils.java
/** * Convert the given HibernateException to an appropriate exception * from the {@code org.springframework.dao} hierarchy. * @param ex HibernateException that occurred * @return the corresponding DataAccessException instance * @see HibernateAccessor#convertHibernateAccessException * @see HibernateTransactionManager#convertHibernateAccessException *///from w w w.ja v a2s. c o m public static DataAccessException convertHibernateAccessException(HibernateException ex) { if (ex instanceof JDBCConnectionException) { return new DataAccessResourceFailureException(ex.getMessage(), ex); } if (ex instanceof SQLGrammarException) { SQLGrammarException jdbcEx = (SQLGrammarException) ex; return new InvalidDataAccessResourceUsageException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } if (ex instanceof QueryTimeoutException) { QueryTimeoutException jdbcEx = (QueryTimeoutException) ex; return new org.springframework.dao.QueryTimeoutException( ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } if (ex instanceof LockAcquisitionException) { LockAcquisitionException jdbcEx = (LockAcquisitionException) ex; return new CannotAcquireLockException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } if (ex instanceof PessimisticLockException) { PessimisticLockException jdbcEx = (PessimisticLockException) ex; return new PessimisticLockingFailureException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } if (ex instanceof ConstraintViolationException) { ConstraintViolationException jdbcEx = (ConstraintViolationException) ex; return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]; constraint [" + jdbcEx.getConstraintName() + "]", ex); } if (ex instanceof DataException) { DataException jdbcEx = (DataException) ex; return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } if (ex instanceof JDBCException) { return new HibernateJdbcException((JDBCException) ex); } // end of JDBCException (subclass) handling if (ex instanceof QueryException) { return new HibernateQueryException((QueryException) ex); } if (ex instanceof NonUniqueResultException) { return new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex); } if (ex instanceof NonUniqueObjectException) { return new DuplicateKeyException(ex.getMessage(), ex); } if (ex instanceof PropertyValueException) { return new DataIntegrityViolationException(ex.getMessage(), ex); } if (ex instanceof PersistentObjectException) { return new InvalidDataAccessApiUsageException(ex.getMessage(), ex); } if (ex instanceof TransientObjectException) { return new InvalidDataAccessApiUsageException(ex.getMessage(), ex); } if (ex instanceof ObjectDeletedException) { return new InvalidDataAccessApiUsageException(ex.getMessage(), ex); } if (ex instanceof UnresolvableObjectException) { return new HibernateObjectRetrievalFailureException((UnresolvableObjectException) ex); } if (ex instanceof WrongClassException) { return new HibernateObjectRetrievalFailureException((WrongClassException) ex); } if (ex instanceof StaleObjectStateException) { return new HibernateOptimisticLockingFailureException((StaleObjectStateException) ex); } if (ex instanceof StaleStateException) { return new HibernateOptimisticLockingFailureException((StaleStateException) ex); } if (ex instanceof OptimisticLockException) { return new HibernateOptimisticLockingFailureException((OptimisticLockException) ex); } // fallback return new HibernateSystemException(ex); }
From source file:org.springframework.orm.hibernate4.fix.SessionFactoryUtils.java
/** * Convert the given HibernateException to an appropriate exception * from the {@code org.springframework.dao} hierarchy. * @param ex HibernateException that occured * @return the corresponding DataAccessException instance * @see HibernateExceptionTranslator#convertHibernateAccessException * @see HibernateTransactionManager#convertHibernateAccessException *//*from w ww . java 2s . com*/ public static DataAccessException convertHibernateAccessException(HibernateException ex) { if (ex instanceof JDBCConnectionException) { return new DataAccessResourceFailureException(ex.getMessage(), ex); } if (ex instanceof SQLGrammarException) { SQLGrammarException jdbcEx = (SQLGrammarException) ex; return new InvalidDataAccessResourceUsageException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } if (ex instanceof LockAcquisitionException) { LockAcquisitionException jdbcEx = (LockAcquisitionException) ex; return new CannotAcquireLockException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } if (ex instanceof ConstraintViolationException) { ConstraintViolationException jdbcEx = (ConstraintViolationException) ex; return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]; constraint [" + jdbcEx.getConstraintName() + "]", ex); } if (ex instanceof DataException) { DataException jdbcEx = (DataException) ex; return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } if (ex instanceof JDBCException) { return new HibernateJdbcException((JDBCException) ex); } // end of JDBCException (subclass) handling if (ex instanceof QueryException) { return new HibernateQueryException((QueryException) ex); } if (ex instanceof NonUniqueResultException) { return new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex); } if (ex instanceof NonUniqueObjectException) { return new DuplicateKeyException(ex.getMessage(), ex); } if (ex instanceof PropertyValueException) { return new DataIntegrityViolationException(ex.getMessage(), ex); } if (ex instanceof PersistentObjectException) { return new InvalidDataAccessApiUsageException(ex.getMessage(), ex); } if (ex instanceof TransientObjectException) { return new InvalidDataAccessApiUsageException(ex.getMessage(), ex); } if (ex instanceof ObjectDeletedException) { return new InvalidDataAccessApiUsageException(ex.getMessage(), ex); } if (ex instanceof UnresolvableObjectException) { return new HibernateObjectRetrievalFailureException((UnresolvableObjectException) ex); } if (ex instanceof WrongClassException) { return new HibernateObjectRetrievalFailureException((WrongClassException) ex); } if (ex instanceof StaleObjectStateException) { return new HibernateOptimisticLockingFailureException((StaleObjectStateException) ex); } if (ex instanceof StaleStateException) { return new HibernateOptimisticLockingFailureException((StaleStateException) ex); } // fallback return new HibernateSystemException(ex); }
From source file:org.springframework.orm.hibernate4.SessionFactoryUtils.java
/** * Convert the given HibernateException to an appropriate exception * from the {@code org.springframework.dao} hierarchy. * @param ex HibernateException that occurred * @return the corresponding DataAccessException instance * @see HibernateExceptionTranslator#convertHibernateAccessException * @see HibernateTransactionManager#convertHibernateAccessException *///ww w. j a va 2s . c o m public static DataAccessException convertHibernateAccessException(HibernateException ex) { if (ex instanceof JDBCConnectionException) { return new DataAccessResourceFailureException(ex.getMessage(), ex); } if (ex instanceof SQLGrammarException) { SQLGrammarException jdbcEx = (SQLGrammarException) ex; return new InvalidDataAccessResourceUsageException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } if (ex instanceof QueryTimeoutException) { QueryTimeoutException jdbcEx = (QueryTimeoutException) ex; return new org.springframework.dao.QueryTimeoutException( ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } if (ex instanceof LockAcquisitionException) { LockAcquisitionException jdbcEx = (LockAcquisitionException) ex; return new CannotAcquireLockException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } if (ex instanceof PessimisticLockException) { PessimisticLockException jdbcEx = (PessimisticLockException) ex; return new PessimisticLockingFailureException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } if (ex instanceof ConstraintViolationException) { ConstraintViolationException jdbcEx = (ConstraintViolationException) ex; return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]; constraint [" + jdbcEx.getConstraintName() + "]", ex); } if (ex instanceof DataException) { DataException jdbcEx = (DataException) ex; return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } if (ex instanceof JDBCException) { return new HibernateJdbcException((JDBCException) ex); } // end of JDBCException (subclass) handling if (ex instanceof QueryException) { return new HibernateQueryException((QueryException) ex); } if (ex instanceof NonUniqueResultException) { return new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex); } if (ex instanceof NonUniqueObjectException) { return new DuplicateKeyException(ex.getMessage(), ex); } if (ex instanceof PropertyValueException) { return new DataIntegrityViolationException(ex.getMessage(), ex); } if (ex instanceof PersistentObjectException) { return new InvalidDataAccessApiUsageException(ex.getMessage(), ex); } if (ex instanceof TransientObjectException) { return new InvalidDataAccessApiUsageException(ex.getMessage(), ex); } if (ex instanceof ObjectDeletedException) { return new InvalidDataAccessApiUsageException(ex.getMessage(), ex); } if (ex instanceof UnresolvableObjectException) { return new HibernateObjectRetrievalFailureException((UnresolvableObjectException) ex); } if (ex instanceof WrongClassException) { return new HibernateObjectRetrievalFailureException((WrongClassException) ex); } if (ex instanceof StaleObjectStateException) { return new HibernateOptimisticLockingFailureException((StaleObjectStateException) ex); } if (ex instanceof StaleStateException) { return new HibernateOptimisticLockingFailureException((StaleStateException) ex); } if (ex instanceof OptimisticEntityLockException) { return new HibernateOptimisticLockingFailureException((OptimisticEntityLockException) ex); } if (ex instanceof PessimisticEntityLockException) { if (ex.getCause() instanceof LockAcquisitionException) { return new CannotAcquireLockException(ex.getMessage(), ex.getCause()); } return new PessimisticLockingFailureException(ex.getMessage(), ex); } // fallback return new HibernateSystemException(ex); }