Search in sources :

Example 11 with JDBCException

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

the class PostgreSQL81DialectTestCase method testDeadlockException.

@Test
public void testDeadlockException() {
    PostgreSQL81Dialect dialect = new PostgreSQL81Dialect();
    SQLExceptionConversionDelegate delegate = dialect.buildSQLExceptionConversionDelegate();
    assertNotNull(delegate);
    JDBCException exception = delegate.convert(new SQLException("Deadlock Detected", "40P01"), "", "");
    assertTrue(exception instanceof LockAcquisitionException);
}
Also used : SQLExceptionConversionDelegate(org.hibernate.exception.spi.SQLExceptionConversionDelegate) JDBCException(org.hibernate.JDBCException) SQLException(java.sql.SQLException) LockAcquisitionException(org.hibernate.exception.LockAcquisitionException) Test(org.junit.Test)

Example 12 with JDBCException

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

the class PessimisticReadSelectLockingStrategy 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 13 with JDBCException

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

the class CriteriaQueryTest method testSubselectWithComponent.

@Test
@SkipForDialect(value = SybaseASE15Dialect.class, strictMatching = true, jiraKey = "HHH-3032", comment = "I was told this is fixed in Sybase ASE 15.7")
public void testSubselectWithComponent() {
    Session session = openSession();
    Transaction t = session.beginTransaction();
    Course course = new Course();
    course.setCourseCode("HIB");
    course.setDescription("Hibernate Training");
    session.persist(course);
    CityState odessaWa = new CityState("Odessa", "WA");
    Student gavin = new Student();
    gavin.setName("Gavin King");
    gavin.setStudentNumber(232);
    gavin.setCityState(odessaWa);
    session.persist(gavin);
    Enrolment enrolment2 = new Enrolment();
    enrolment2.setCourse(course);
    enrolment2.setCourseCode(course.getCourseCode());
    enrolment2.setSemester((short) 3);
    enrolment2.setYear((short) 1998);
    enrolment2.setStudent(gavin);
    enrolment2.setStudentNumber(gavin.getStudentNumber());
    gavin.getEnrolments().add(enrolment2);
    session.persist(enrolment2);
    DetachedCriteria dc = DetachedCriteria.forClass(Student.class).add(Property.forName("cityState").eq(odessaWa)).setProjection(Property.forName("cityState"));
    session.createCriteria(Student.class).add(Subqueries.exists(dc)).list();
    t.commit();
    session.close();
    session = openSession();
    t = session.beginTransaction();
    try {
        session.createCriteria(Student.class).add(Subqueries.propertyEqAll("cityState", dc)).list();
        fail("should have failed because cannot compare subquery results with multiple columns");
    } catch (QueryException ex) {
    // expected
    } finally {
        t.rollback();
        session.close();
    }
    session = openSession();
    t = session.beginTransaction();
    try {
        session.createCriteria(Student.class).add(Property.forName("cityState").eqAll(dc)).list();
        fail("should have failed because cannot compare subquery results with multiple columns");
    } catch (QueryException ex) {
    // expected
    } finally {
        t.rollback();
        session.close();
    }
    session = openSession();
    t = session.beginTransaction();
    try {
        session.createCriteria(Student.class).add(Subqueries.in(odessaWa, dc)).list();
        fail("should have failed because cannot compare subquery results with multiple columns");
    } catch (JDBCException ex) {
    // expected
    } finally {
        t.rollback();
        session.close();
    }
    session = openSession();
    t = session.beginTransaction();
    DetachedCriteria dc2 = DetachedCriteria.forClass(Student.class, "st1").add(Property.forName("st1.cityState").eqProperty("st2.cityState")).setProjection(Property.forName("cityState"));
    try {
        session.createCriteria(Student.class, "st2").add(Subqueries.eq(odessaWa, dc2)).list();
        fail("should have failed because cannot compare subquery results with multiple columns");
    } catch (JDBCException ex) {
    // expected
    } finally {
        t.rollback();
        session.close();
    }
    session = openSession();
    t = session.beginTransaction();
    DetachedCriteria dc3 = DetachedCriteria.forClass(Student.class, "st").createCriteria("enrolments").createCriteria("course").add(Property.forName("description").eq("Hibernate Training")).setProjection(Property.forName("st.cityState"));
    try {
        session.createCriteria(Enrolment.class, "e").add(Subqueries.eq(odessaWa, dc3)).list();
        fail("should have failed because cannot compare subquery results with multiple columns");
    } catch (JDBCException ex) {
    // expected
    } finally {
        t.rollback();
        session.close();
    }
    session = openSession();
    t = session.beginTransaction();
    session.delete(enrolment2);
    session.delete(gavin);
    session.delete(course);
    t.commit();
    session.close();
}
Also used : QueryException(org.hibernate.QueryException) Transaction(org.hibernate.Transaction) JDBCException(org.hibernate.JDBCException) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Example 14 with JDBCException

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

the class BasicConnectionTest method testBasicJdbcUsage.

@Test
public void testBasicJdbcUsage() throws JDBCException {
    Session session = openSession();
    SessionImplementor sessionImpl = (SessionImplementor) session;
    JdbcCoordinator jdbcCoord = sessionImpl.getJdbcCoordinator();
    try {
        Statement statement = jdbcCoord.getStatementPreparer().createStatement();
        String dropSql = getDialect().getDropTableString("SANDBOX_JDBC_TST");
        try {
            jdbcCoord.getResultSetReturn().execute(statement, dropSql);
        } catch (Exception e) {
        // ignore if the DB doesn't support "if exists" and the table doesn't exist
        }
        jdbcCoord.getResultSetReturn().execute(statement, "create table SANDBOX_JDBC_TST ( ID integer, NAME varchar(100) )");
        assertTrue(getResourceRegistry(jdbcCoord).hasRegisteredResources());
        assertTrue(jdbcCoord.getLogicalConnection().isPhysicallyConnected());
        getResourceRegistry(jdbcCoord).release(statement);
        assertFalse(getResourceRegistry(jdbcCoord).hasRegisteredResources());
        // after_transaction specified
        assertTrue(jdbcCoord.getLogicalConnection().isPhysicallyConnected());
        PreparedStatement ps = jdbcCoord.getStatementPreparer().prepareStatement("insert into SANDBOX_JDBC_TST( ID, NAME ) values ( ?, ? )");
        ps.setLong(1, 1);
        ps.setString(2, "name");
        jdbcCoord.getResultSetReturn().execute(ps);
        ps = jdbcCoord.getStatementPreparer().prepareStatement("select * from SANDBOX_JDBC_TST");
        jdbcCoord.getResultSetReturn().extract(ps);
        assertTrue(getResourceRegistry(jdbcCoord).hasRegisteredResources());
    } catch (SQLException e) {
        fail("incorrect exception type : sqlexception");
    } finally {
        try {
            session.doWork(connection -> {
                final Statement stmnt = connection.createStatement();
                stmnt.execute(getDialect().getDropTableString("SANDBOX_JDBC_TST"));
            });
        } finally {
            session.close();
        }
    }
    assertFalse(getResourceRegistry(jdbcCoord).hasRegisteredResources());
}
Also used : JdbcCoordinator(org.hibernate.engine.jdbc.spi.JdbcCoordinator) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException) JDBCException(org.hibernate.JDBCException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 15 with JDBCException

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

the class BasicConnectionTest method testExceptionHandling.

@Test
public void testExceptionHandling() {
    Session session = openSession();
    SessionImplementor sessionImpl = (SessionImplementor) session;
    boolean caught = false;
    try {
        PreparedStatement ps = sessionImpl.getJdbcCoordinator().getStatementPreparer().prepareStatement("select count(*) from NON_EXISTENT");
        sessionImpl.getJdbcCoordinator().getResultSetReturn().execute(ps);
    } catch (JDBCException ok) {
        caught = true;
    } finally {
        session.close();
    }
    assertTrue("The connection did not throw a JDBCException as expected", caught);
}
Also used : JDBCException(org.hibernate.JDBCException) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) PreparedStatement(java.sql.PreparedStatement) 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