Search in sources :

Example 1 with JDBCException

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

the class BeforeCompletionFailureTest method testUniqueConstraintViolationDuringManagedFlush.

@Test
@TestForIssue(jiraKey = "HHH-9888")
public void testUniqueConstraintViolationDuringManagedFlush() throws Exception {
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // set up test data
    Session session = openSession();
    session.getTransaction().begin();
    session.save(newEntity(1));
    session.getTransaction().commit();
    session.close();
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // do the test
    final TransactionManager tm = JtaPlatformStandardTestingImpl.INSTANCE.transactionManager();
    assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
    // begin the transaction ("CMT" style)
    tm.begin();
    session = openSession();
    session.save(newEntity(2));
    // which should lead to the UK violation
    try {
        tm.commit();
        fail("Expecting a failure from JTA commit");
    } catch (RollbackException expected) {
        log.info("Test encountered expected JTA RollbackException; looking for nested JDBCException", expected);
        boolean violationExceptionFound = false;
        Throwable cause = expected;
        while (cause != null) {
            if (cause instanceof JDBCException) {
                log.info("Found JDBCException, assuming related to UK violation", cause);
                violationExceptionFound = true;
                break;
            }
            cause = cause.getCause();
        }
        if (!violationExceptionFound) {
            fail("Did not find JDBCException in JTA RollbackException chain");
        }
    } finally {
        if (!((SessionImplementor) session).isClosed()) {
            session.close();
        }
    }
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // clean up test data
    session = openSession();
    session.getTransaction().begin();
    session.createQuery("delete SimpleEntity").executeUpdate();
    session.getTransaction().commit();
    session.close();
}
Also used : JDBCException(org.hibernate.JDBCException) TransactionManager(javax.transaction.TransactionManager) RollbackException(javax.transaction.RollbackException) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 2 with JDBCException

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

the class PostgreSQL81DialectTestCase method testTimeoutException.

@Test
public void testTimeoutException() {
    PostgreSQL81Dialect dialect = new PostgreSQL81Dialect();
    SQLExceptionConversionDelegate delegate = dialect.buildSQLExceptionConversionDelegate();
    assertNotNull(delegate);
    JDBCException exception = delegate.convert(new SQLException("Lock Not Available", "55P03"), "", "");
    assertTrue(exception instanceof PessimisticLockException);
}
Also used : SQLExceptionConversionDelegate(org.hibernate.exception.spi.SQLExceptionConversionDelegate) JDBCException(org.hibernate.JDBCException) SQLException(java.sql.SQLException) PessimisticLockException(org.hibernate.PessimisticLockException) Test(org.junit.Test)

Example 3 with JDBCException

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

the class GeneralWorkTest method testSQLExceptionThrowing.

@Test
public void testSQLExceptionThrowing() {
    final Session session = openSession();
    session.beginTransaction();
    try {
        session.doWork(new Work() {

            public void execute(Connection connection) throws SQLException {
                Statement statement = null;
                try {
                    statement = ((SessionImplementor) session).getJdbcCoordinator().getStatementPreparer().createStatement();
                    ((SessionImplementor) session).getJdbcCoordinator().getResultSetReturn().extract(statement, "select * from non_existent");
                } finally {
                    releaseQuietly(((SessionImplementor) session), statement);
                }
            }
        });
        fail("expecting exception");
    } catch (JDBCException expected) {
    // expected outcome
    }
    session.getTransaction().commit();
    session.close();
}
Also used : JDBCException(org.hibernate.JDBCException) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Work(org.hibernate.jdbc.Work) ReturningWork(org.hibernate.jdbc.ReturningWork) Connection(java.sql.Connection) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Session(org.hibernate.Session) Test(org.junit.Test)

Example 4 with JDBCException

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

the class NonBatchingBatch method addToBatch.

@Override
public void addToBatch() {
    notifyObserversImplicitExecution();
    for (Map.Entry<String, PreparedStatement> entry : getStatements().entrySet()) {
        try {
            final PreparedStatement statement = entry.getValue();
            final int rowCount = jdbcCoordinator.getResultSetReturn().executeUpdate(statement);
            getKey().getExpectation().verifyOutcome(rowCount, statement, 0);
            jdbcCoordinator.getResourceRegistry().release(statement);
            jdbcCoordinator.afterStatementExecution();
        } catch (SQLException e) {
            abortBatch();
            throw sqlExceptionHelper().convert(e, "could not execute non-batched batch statement", entry.getKey());
        } catch (JDBCException e) {
            abortBatch();
            throw e;
        }
    }
    getStatements().clear();
}
Also used : JDBCException(org.hibernate.JDBCException) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Map(java.util.Map)

Example 5 with JDBCException

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

the class PessimisticReadUpdateLockingStrategy 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)

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