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);
}
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);
}
}
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();
}
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());
}
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);
}
Aggregations