use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class NonBatchingBatchFailureTest method testBasicInsertion.
@Test
public void testBasicInsertion() {
Session session = openSession();
session.getTransaction().begin();
try {
session.persist(new User(1, "ok"));
session.persist(new User(2, null));
session.persist(new User(3, "ok"));
// the flush should fail
session.flush();
fail("Expecting failed flush");
} catch (Exception expected) {
System.out.println("Caught expected exception : " + expected);
expected.printStackTrace(System.out);
try {
//at this point the transaction is still active but the batch should have been aborted (have to use reflection to get at the field)
SessionImplementor sessionImplementor = (SessionImplementor) session;
Field field = sessionImplementor.getJdbcCoordinator().getClass().getDeclaredField("currentBatch");
field.setAccessible(true);
Batch batch = (Batch) field.get(sessionImplementor.getJdbcCoordinator());
if (batch == null) {
throw new Exception("Current batch was null");
} else {
//make sure it's actually a batching impl
assertEquals(NonBatchingBatch.class, batch.getClass());
field = AbstractBatchImpl.class.getDeclaredField("statements");
field.setAccessible(true);
//check to see that there aren't any statements queued up (this can be an issue if using SavePoints)
assertEquals(0, ((Map) field.get(batch)).size());
}
} catch (Exception fieldException) {
fail("Couldn't inspect field " + fieldException.getMessage());
}
} finally {
session.getTransaction().rollback();
session.close();
}
}
use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class SQLServerDialectTest method testMaxResultsSqlServerWithCaseSensitiveCollation.
@Test
@TestForIssue(jiraKey = "HHH-7198")
public void testMaxResultsSqlServerWithCaseSensitiveCollation() throws Exception {
final Session s1 = openSession();
s1.beginTransaction();
String defaultCollationName = s1.doReturningWork(connection -> {
String databaseName = connection.getCatalog();
Statement st = ((SessionImplementor) s1).getJdbcCoordinator().getStatementPreparer().createStatement();
ResultSet rs = ((SessionImplementor) s1).getJdbcCoordinator().getResultSetReturn().extract(st, "SELECT collation_name FROM sys.databases WHERE name = '" + databaseName + "';");
while (rs.next()) {
return rs.getString("collation_name");
}
throw new AssertionError("can't get collation name of database " + databaseName);
});
s1.getTransaction().commit();
s1.close();
Session s2 = openSession();
Transaction tx = s2.beginTransaction();
String databaseName = s2.doReturningWork(new ReturningWork<String>() {
@Override
public String execute(Connection connection) throws SQLException {
return connection.getCatalog();
}
});
s2.createNativeQuery("ALTER DATABASE " + databaseName + " set single_user with rollback immediate").executeUpdate();
s2.createNativeQuery("ALTER DATABASE " + databaseName + " COLLATE Latin1_General_CS_AS").executeUpdate();
s2.createNativeQuery("ALTER DATABASE " + databaseName + " set multi_user").executeUpdate();
for (int i = 1; i <= 20; i++) {
s2.persist(new Product2(i, "Kit" + i));
}
s2.flush();
s2.clear();
List list = s2.createQuery("from Product2 where description like 'Kit%'").setFirstResult(2).setMaxResults(2).list();
assertEquals(2, list.size());
tx.rollback();
s2.close();
executorService.execute(() -> {
doInHibernate(this::sessionFactory, s3 -> {
s3.createNativeQuery("ALTER DATABASE " + databaseName + " set single_user with rollback immediate").executeUpdate();
s3.createNativeQuery("ALTER DATABASE " + databaseName + " COLLATE " + defaultCollationName).executeUpdate();
s3.createNativeQuery("ALTER DATABASE " + databaseName + " set multi_user").executeUpdate();
});
});
}
use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class BatchingTest method testNonBatchingUsage.
@Test
public void testNonBatchingUsage() throws Exception {
Session session = openSession();
SessionImplementor sessionImpl = (SessionImplementor) session;
final JdbcCoordinator jdbcCoordinator = sessionImpl.getJdbcCoordinator();
LogicalConnectionImplementor logicalConnection = jdbcCoordinator.getLogicalConnection();
// set up some tables to use
Statement statement = jdbcCoordinator.getStatementPreparer().createStatement();
String dropSql = getDialect().getDropTableString("SANDBOX_JDBC_TST");
try {
jdbcCoordinator.getResultSetReturn().execute(statement, dropSql);
} catch (Exception e) {
// ignore if the DB doesn't support "if exists" and the table doesn't exist
}
jdbcCoordinator.getResultSetReturn().execute(statement, "create table SANDBOX_JDBC_TST ( ID integer, NAME varchar(100) )");
assertTrue(jdbcCoordinator.getResourceRegistry().hasRegisteredResources());
assertTrue(logicalConnection.isPhysicallyConnected());
jdbcCoordinator.getResourceRegistry().release(statement);
assertFalse(jdbcCoordinator.getResourceRegistry().hasRegisteredResources());
// after_transaction specified
assertTrue(logicalConnection.isPhysicallyConnected());
// ok, now we can get down to it...
// same as Session#getTransaction
Transaction txn = session.getTransaction();
txn.begin();
final String insertSql = "insert into SANDBOX_JDBC_TST( ID, NAME ) values ( ?, ? )";
final BatchBuilder batchBuilder = new BatchBuilderImpl(-1);
final BatchKey batchKey = new BasicBatchKey("this", Expectations.BASIC);
final Batch insertBatch = batchBuilder.buildBatch(batchKey, jdbcCoordinator);
final JournalingBatchObserver batchObserver = new JournalingBatchObserver();
insertBatch.addObserver(batchObserver);
assertTrue("unexpected Batch impl", NonBatchingBatch.class.isInstance(insertBatch));
PreparedStatement insert = insertBatch.getBatchStatement(insertSql, false);
insert.setLong(1, 1);
insert.setString(2, "name");
assertEquals(0, batchObserver.getExplicitExecutionCount());
assertEquals(0, batchObserver.getImplicitExecutionCount());
insertBatch.addToBatch();
assertEquals(0, batchObserver.getExplicitExecutionCount());
assertEquals(1, batchObserver.getImplicitExecutionCount());
assertFalse(jdbcCoordinator.getResourceRegistry().hasRegisteredResources());
insertBatch.execute();
assertEquals(1, batchObserver.getExplicitExecutionCount());
assertEquals(1, batchObserver.getImplicitExecutionCount());
assertFalse(jdbcCoordinator.getResourceRegistry().hasRegisteredResources());
insertBatch.release();
txn.commit();
session.close();
}
use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class GeneralWorkTest method testGeneralUsage.
@Test
public void testGeneralUsage() throws Throwable {
final Session session = openSession();
session.beginTransaction();
session.doWork(new Work() {
public void execute(Connection connection) throws SQLException {
// in this current form, users must handle try/catches themselves for proper resource release
Statement statement = null;
try {
statement = ((SessionImplementor) session).getJdbcCoordinator().getStatementPreparer().createStatement();
ResultSet resultSet = null;
try {
resultSet = ((SessionImplementor) session).getJdbcCoordinator().getResultSetReturn().extract(statement, "select * from T_JDBC_PERSON");
} finally {
releaseQuietly(((SessionImplementor) session), resultSet, statement);
}
try {
((SessionImplementor) session).getJdbcCoordinator().getResultSetReturn().extract(statement, "select * from T_JDBC_BOAT");
} finally {
releaseQuietly(((SessionImplementor) session), resultSet, statement);
}
} finally {
releaseQuietly(((SessionImplementor) session), statement);
}
}
});
session.getTransaction().commit();
session.close();
}
use of org.hibernate.engine.spi.SessionImplementor 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());
}
Aggregations