Search in sources :

Example 6 with JDBCException

use of org.hibernate.JDBCException in project hibernate-orm by hibernate.

the class PessimisticWriteSelectLockingStrategy method lock.

@Override
public void lock(Serializable id, Object version, Object object, int timeout, SharedSessionContractImplementor session) {
    final String sql = determineSql(timeout);
    final SessionFactoryImplementor factory = session.getFactory();
    try {
        try {
            final PreparedStatement st = session.getJdbcCoordinator().getStatementPreparer().prepareStatement(sql);
            try {
                getLockable().getIdentifierType().nullSafeSet(st, id, 1, session);
                if (getLockable().isVersioned()) {
                    getLockable().getVersionType().nullSafeSet(st, version, getLockable().getIdentifierType().getColumnSpan(factory) + 1, session);
                }
                final ResultSet rs = session.getJdbcCoordinator().getResultSetReturn().extract(st);
                try {
                    if (!rs.next()) {
                        if (factory.getStatistics().isStatisticsEnabled()) {
                            factory.getStatistics().optimisticFailure(getLockable().getEntityName());
                        }
                        throw new StaleObjectStateException(getLockable().getEntityName(), id);
                    }
                } finally {
                    session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release(rs, st);
                }
            } finally {
                session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release(st);
                session.getJdbcCoordinator().afterStatementExecution();
            }
        } catch (SQLException e) {
            throw session.getJdbcServices().getSqlExceptionHelper().convert(e, "could not lock: " + MessageHelper.infoString(getLockable(), id, session.getFactory()), sql);
        }
    } catch (JDBCException e) {
        throw new PessimisticEntityLockException(object, "could not obtain pessimistic lock", e);
    }
}
Also used : JDBCException(org.hibernate.JDBCException) SQLException(java.sql.SQLException) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) StaleObjectStateException(org.hibernate.StaleObjectStateException)

Example 7 with JDBCException

use of org.hibernate.JDBCException in project hibernate-orm by hibernate.

the class PessimisticWriteUpdateLockingStrategy method lock.

@Override
public void lock(Serializable id, Object version, Object object, int timeout, SharedSessionContractImplementor session) {
    if (!lockable.isVersioned()) {
        throw new HibernateException("write locks via update not supported for non-versioned entities [" + lockable.getEntityName() + "]");
    }
    final SessionFactoryImplementor factory = session.getFactory();
    try {
        try {
            final PreparedStatement st = session.getJdbcCoordinator().getStatementPreparer().prepareStatement(sql);
            try {
                lockable.getVersionType().nullSafeSet(st, version, 1, session);
                int offset = 2;
                lockable.getIdentifierType().nullSafeSet(st, id, offset, session);
                offset += lockable.getIdentifierType().getColumnSpan(factory);
                if (lockable.isVersioned()) {
                    lockable.getVersionType().nullSafeSet(st, version, offset, session);
                }
                final int affected = session.getJdbcCoordinator().getResultSetReturn().executeUpdate(st);
                // todo:  should this instead check for exactly one row modified?
                if (affected < 0) {
                    if (factory.getStatistics().isStatisticsEnabled()) {
                        factory.getStatistics().optimisticFailure(lockable.getEntityName());
                    }
                    throw new StaleObjectStateException(lockable.getEntityName(), id);
                }
            } finally {
                session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release(st);
                session.getJdbcCoordinator().afterStatementExecution();
            }
        } catch (SQLException e) {
            throw session.getJdbcServices().getSqlExceptionHelper().convert(e, "could not lock: " + MessageHelper.infoString(lockable, id, session.getFactory()), sql);
        }
    } catch (JDBCException e) {
        throw new PessimisticEntityLockException(object, "could not obtain pessimistic lock", e);
    }
}
Also used : JDBCException(org.hibernate.JDBCException) HibernateException(org.hibernate.HibernateException) SQLException(java.sql.SQLException) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) PreparedStatement(java.sql.PreparedStatement) StaleObjectStateException(org.hibernate.StaleObjectStateException)

Example 8 with JDBCException

use of org.hibernate.JDBCException in project hibernate-orm by hibernate.

the class ContextualLobCreator method createNClob.

@Override
public NClob createNClob(String string) {
    try {
        final NClob nclob = createNClob();
        nclob.setString(1, string);
        return nclob;
    } catch (SQLException e) {
        throw new JDBCException("Unable to set NCLOB string after creation", e);
    }
}
Also used : NClob(java.sql.NClob) JDBCException(org.hibernate.JDBCException) SQLException(java.sql.SQLException)

Example 9 with JDBCException

use of org.hibernate.JDBCException in project hibernate-orm by hibernate.

the class ContextualLobCreator method createBlob.

@Override
public Blob createBlob(byte[] bytes) {
    try {
        final Blob blob = createBlob();
        blob.setBytes(1, bytes);
        return blob;
    } catch (SQLException e) {
        throw new JDBCException("Unable to set BLOB bytes after creation", e);
    }
}
Also used : Blob(java.sql.Blob) JDBCException(org.hibernate.JDBCException) SQLException(java.sql.SQLException)

Example 10 with JDBCException

use of org.hibernate.JDBCException in project hibernate-orm by hibernate.

the class EmbeddableIntegratorTest method testWithoutIntegrator.

/**
 * Throws a mapping exception because DollarValue is not mapped
 */
@Test
public void testWithoutIntegrator() {
    SessionFactory sf = new Configuration().addAnnotatedClass(Investor.class).setProperty("hibernate.hbm2ddl.auto", "create-drop").buildSessionFactory();
    try {
        Session sess = sf.openSession();
        try {
            sess.getTransaction().begin();
            Investor myInv = getInvestor();
            myInv.setId(1L);
            sess.save(myInv);
            sess.flush();
            fail("A JDBCException expected");
            sess.clear();
            Investor inv = (Investor) sess.get(Investor.class, 1L);
            assertEquals(new BigDecimal("100"), inv.getInvestments().get(0).getAmount().getAmount());
        } catch (PersistenceException e) {
            assertTyping(JDBCException.class, e.getCause());
            sess.getTransaction().rollback();
        }
        sess.close();
    } finally {
        sf.close();
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) Configuration(org.hibernate.cfg.Configuration) JDBCException(org.hibernate.JDBCException) PersistenceException(javax.persistence.PersistenceException) BigDecimal(java.math.BigDecimal) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

JDBCException (org.hibernate.JDBCException)22 SQLException (java.sql.SQLException)13 Test (org.junit.Test)11 Session (org.hibernate.Session)9 PreparedStatement (java.sql.PreparedStatement)7 StaleObjectStateException (org.hibernate.StaleObjectStateException)5 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)3 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)3 FeedMetadata (com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata)2 NifiFeed (com.thinkbiganalytics.feedmgr.rest.model.NifiFeed)2 FeedCleanupFailedException (com.thinkbiganalytics.feedmgr.service.FeedCleanupFailedException)2 FeedCleanupTimeoutException (com.thinkbiganalytics.feedmgr.service.FeedCleanupTimeoutException)2 DeployFeedException (com.thinkbiganalytics.feedmgr.service.feed.DeployFeedException)2 DuplicateFeedNameException (com.thinkbiganalytics.feedmgr.service.feed.DuplicateFeedNameException)2 FeedCurrentlyRunningException (com.thinkbiganalytics.feedmgr.service.feed.reindexing.FeedCurrentlyRunningException)2 FeedHistoryDataReindexingNotEnabledException (com.thinkbiganalytics.feedmgr.service.feed.reindexing.FeedHistoryDataReindexingNotEnabledException)2 FeedNotFoundException (com.thinkbiganalytics.metadata.api.feed.FeedNotFoundException)2 VersionNotFoundException (com.thinkbiganalytics.metadata.api.versioning.VersionNotFoundException)2 NifiClientRuntimeException (com.thinkbiganalytics.nifi.rest.client.NifiClientRuntimeException)2 NifiConnectionException (com.thinkbiganalytics.nifi.rest.client.NifiConnectionException)2