use of org.hibernate.engine.jdbc.spi.JdbcCoordinator in project hibernate-orm by hibernate.
the class BatchingTest method testBatchingUsage.
@Test
public void testBatchingUsage() 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 BatchBuilder batchBuilder = new BatchBuilderImpl(2);
final BatchKey batchKey = new BasicBatchKey("this", Expectations.BASIC);
final Batch insertBatch = batchBuilder.buildBatch(batchKey, jdbcCoordinator);
assertTrue("unexpected Batch impl", BatchingBatch.class.isInstance(insertBatch));
final JournalingBatchObserver batchObserver = new JournalingBatchObserver();
insertBatch.addObserver(batchObserver);
final String insertSql = "insert into SANDBOX_JDBC_TST( ID, NAME ) values ( ?, ? )";
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(0, batchObserver.getImplicitExecutionCount());
assertTrue(jdbcCoordinator.getResourceRegistry().hasRegisteredResources());
PreparedStatement insert2 = insertBatch.getBatchStatement(insertSql, false);
assertSame(insert, insert2);
insert = insert2;
insert.setLong(1, 2);
insert.setString(2, "another name");
assertEquals(0, batchObserver.getExplicitExecutionCount());
assertEquals(0, batchObserver.getImplicitExecutionCount());
insertBatch.addToBatch();
assertEquals(0, batchObserver.getExplicitExecutionCount());
assertEquals(1, batchObserver.getImplicitExecutionCount());
assertTrue(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.jdbc.spi.JdbcCoordinator in project hibernate-orm by hibernate.
the class CursorFromCallableTest method executeStatement.
private void executeStatement(final String sql) {
final Session session = openSession();
session.getTransaction().begin();
session.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
final JdbcCoordinator jdbcCoordinator = ((SessionImplementor) session).getJdbcCoordinator();
final StatementPreparer statementPreparer = jdbcCoordinator.getStatementPreparer();
final ResultSetReturn resultSetReturn = jdbcCoordinator.getResultSetReturn();
PreparedStatement preparedStatement = null;
try {
preparedStatement = statementPreparer.prepareStatement(sql);
resultSetReturn.execute(preparedStatement);
} finally {
if (preparedStatement != null) {
try {
jdbcCoordinator.getResourceRegistry().release(preparedStatement);
} catch (Throwable ignore) {
// ignore...
}
}
}
}
});
session.getTransaction().commit();
session.close();
}
Aggregations