Search in sources :

Example 11 with DataAccessResourceFailureException

use of org.springframework.dao.DataAccessResourceFailureException in project spring-framework by spring-projects.

the class OpenSessionInViewFilter method openSession.

/**
	 * Open a Session for the SessionFactory that this filter uses.
	 * <p>The default implementation delegates to the {@link SessionFactory#openSession}
	 * method and sets the {@link Session}'s flush mode to "MANUAL".
	 * @param sessionFactory the SessionFactory that this filter uses
	 * @return the Session to use
	 * @throws DataAccessResourceFailureException if the Session could not be created
	 * @see FlushMode#MANUAL
	 */
@SuppressWarnings("deprecation")
protected Session openSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException {
    try {
        Session session = sessionFactory.openSession();
        session.setFlushMode(FlushMode.MANUAL);
        return session;
    } catch (HibernateException ex) {
        throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex);
    }
}
Also used : HibernateException(org.hibernate.HibernateException) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) Session(org.hibernate.Session)

Example 12 with DataAccessResourceFailureException

use of org.springframework.dao.DataAccessResourceFailureException in project spring-framework by spring-projects.

the class OpenSessionInterceptor method openSession.

/**
	 * Open a Session for the SessionFactory that this interceptor uses.
	 * <p>The default implementation delegates to the {@link SessionFactory#openSession}
	 * method and sets the {@link Session}'s flush mode to "MANUAL".
	 * @return the Session to use
	 * @throws DataAccessResourceFailureException if the Session could not be created
	 * @see FlushMode#MANUAL
	 */
@SuppressWarnings("deprecation")
protected Session openSession() throws DataAccessResourceFailureException {
    try {
        Session session = getSessionFactory().openSession();
        session.setFlushMode(FlushMode.MANUAL);
        return session;
    } catch (HibernateException ex) {
        throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex);
    }
}
Also used : HibernateException(org.hibernate.HibernateException) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) Session(org.hibernate.Session)

Example 13 with DataAccessResourceFailureException

use of org.springframework.dao.DataAccessResourceFailureException in project spring-framework by spring-projects.

the class OpenEntityManagerInViewFilter method doFilterInternal.

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    EntityManagerFactory emf = lookupEntityManagerFactory(request);
    boolean participate = false;
    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
    String key = getAlreadyFilteredAttributeName();
    if (TransactionSynchronizationManager.hasResource(emf)) {
        // Do not modify the EntityManager: just set the participate flag.
        participate = true;
    } else {
        boolean isFirstRequest = !isAsyncDispatch(request);
        if (isFirstRequest || !applyEntityManagerBindingInterceptor(asyncManager, key)) {
            logger.debug("Opening JPA EntityManager in OpenEntityManagerInViewFilter");
            try {
                EntityManager em = createEntityManager(emf);
                EntityManagerHolder emHolder = new EntityManagerHolder(em);
                TransactionSynchronizationManager.bindResource(emf, emHolder);
                AsyncRequestInterceptor interceptor = new AsyncRequestInterceptor(emf, emHolder);
                asyncManager.registerCallableInterceptor(key, interceptor);
                asyncManager.registerDeferredResultInterceptor(key, interceptor);
            } catch (PersistenceException ex) {
                throw new DataAccessResourceFailureException("Could not create JPA EntityManager", ex);
            }
        }
    }
    try {
        filterChain.doFilter(request, response);
    } finally {
        if (!participate) {
            EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager.unbindResource(emf);
            if (!isAsyncStarted(request)) {
                logger.debug("Closing JPA EntityManager in OpenEntityManagerInViewFilter");
                EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager());
            }
        }
    }
}
Also used : WebAsyncManager(org.springframework.web.context.request.async.WebAsyncManager) EntityManager(javax.persistence.EntityManager) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) EntityManagerFactory(javax.persistence.EntityManagerFactory) PersistenceException(javax.persistence.PersistenceException) EntityManagerHolder(org.springframework.orm.jpa.EntityManagerHolder)

Example 14 with DataAccessResourceFailureException

use of org.springframework.dao.DataAccessResourceFailureException in project spring-framework by spring-projects.

the class OpenEntityManagerInViewInterceptor method preHandle.

@Override
public void preHandle(WebRequest request) throws DataAccessException {
    String participateAttributeName = getParticipateAttributeName();
    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
    if (asyncManager.hasConcurrentResult()) {
        if (applyCallableInterceptor(asyncManager, participateAttributeName)) {
            return;
        }
    }
    if (TransactionSynchronizationManager.hasResource(getEntityManagerFactory())) {
        // Do not modify the EntityManager: just mark the request accordingly.
        Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
        int newCount = (count != null ? count + 1 : 1);
        request.setAttribute(getParticipateAttributeName(), newCount, WebRequest.SCOPE_REQUEST);
    } else {
        logger.debug("Opening JPA EntityManager in OpenEntityManagerInViewInterceptor");
        try {
            EntityManager em = createEntityManager();
            EntityManagerHolder emHolder = new EntityManagerHolder(em);
            TransactionSynchronizationManager.bindResource(getEntityManagerFactory(), emHolder);
            AsyncRequestInterceptor interceptor = new AsyncRequestInterceptor(getEntityManagerFactory(), emHolder);
            asyncManager.registerCallableInterceptor(participateAttributeName, interceptor);
            asyncManager.registerDeferredResultInterceptor(participateAttributeName, interceptor);
        } catch (PersistenceException ex) {
            throw new DataAccessResourceFailureException("Could not create JPA EntityManager", ex);
        }
    }
}
Also used : WebAsyncManager(org.springframework.web.context.request.async.WebAsyncManager) EntityManager(javax.persistence.EntityManager) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) PersistenceException(javax.persistence.PersistenceException) EntityManagerHolder(org.springframework.orm.jpa.EntityManagerHolder)

Example 15 with DataAccessResourceFailureException

use of org.springframework.dao.DataAccessResourceFailureException in project spring-framework by spring-projects.

the class CciTemplate method execute.

@Override
public <T> T execute(ConnectionCallback<T> action) throws DataAccessException {
    Assert.notNull(action, "Callback object must not be null");
    Connection con = ConnectionFactoryUtils.getConnection(getConnectionFactory(), getConnectionSpec());
    try {
        return action.doInConnection(con, getConnectionFactory());
    } catch (NotSupportedException ex) {
        throw new CciOperationNotSupportedException("CCI operation not supported by connector", ex);
    } catch (ResourceException ex) {
        throw new DataAccessResourceFailureException("CCI operation failed", ex);
    } catch (SQLException ex) {
        throw new InvalidResultSetAccessException("Parsing of CCI ResultSet failed", ex);
    } finally {
        ConnectionFactoryUtils.releaseConnection(con, getConnectionFactory());
    }
}
Also used : CciOperationNotSupportedException(org.springframework.jca.cci.CciOperationNotSupportedException) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) SQLException(java.sql.SQLException) Connection(javax.resource.cci.Connection) ResourceException(javax.resource.ResourceException) InvalidResultSetAccessException(org.springframework.jca.cci.InvalidResultSetAccessException) RecordTypeNotSupportedException(org.springframework.jca.cci.RecordTypeNotSupportedException) NotSupportedException(javax.resource.NotSupportedException) CciOperationNotSupportedException(org.springframework.jca.cci.CciOperationNotSupportedException)

Aggregations

DataAccessResourceFailureException (org.springframework.dao.DataAccessResourceFailureException)36 SQLException (java.sql.SQLException)10 Connection (java.sql.Connection)6 IOException (java.io.IOException)5 HibernateException (org.hibernate.HibernateException)4 Session (org.hibernate.Session)4 File (java.io.File)3 ResultSet (java.sql.ResultSet)3 Statement (java.sql.Statement)3 Test (org.junit.Test)3 EventProxyException (org.opennms.netmgt.events.api.EventProxyException)3 EventBuilder (org.opennms.netmgt.model.events.EventBuilder)3 FileOutputStream (java.io.FileOutputStream)2 Clob (java.sql.Clob)2 DatabaseMetaData (java.sql.DatabaseMetaData)2 Date (java.util.Date)2 EntityManager (javax.persistence.EntityManager)2 PersistenceException (javax.persistence.PersistenceException)2 PrefabGraph (org.opennms.netmgt.model.PrefabGraph)2 DatabaseMetaDataCallback (org.springframework.jdbc.support.DatabaseMetaDataCallback)2