Search in sources :

Example 1 with Batch

use of org.hibernate.engine.jdbc.batch.spi.Batch in project hibernate-orm by hibernate.

the class JdbcCoordinatorTest method testConnectionClose.

@Test
public void testConnectionClose() throws NoSuchFieldException, IllegalAccessException, SQLException {
    Connection connection = Mockito.mock(Connection.class);
    JdbcSessionOwner sessionOwner = Mockito.mock(JdbcSessionOwner.class);
    JdbcConnectionAccess jdbcConnectionAccess = Mockito.mock(JdbcConnectionAccess.class);
    when(jdbcConnectionAccess.obtainConnection()).thenReturn(connection);
    when(jdbcConnectionAccess.supportsAggressiveRelease()).thenReturn(false);
    JdbcSessionContext sessionContext = Mockito.mock(JdbcSessionContext.class);
    when(sessionOwner.getJdbcSessionContext()).thenReturn(sessionContext);
    when(sessionOwner.getJdbcConnectionAccess()).thenReturn(jdbcConnectionAccess);
    ServiceRegistry serviceRegistry = Mockito.mock(ServiceRegistry.class);
    when(sessionContext.getServiceRegistry()).thenReturn(serviceRegistry);
    when(sessionContext.getPhysicalConnectionHandlingMode()).thenReturn(PhysicalConnectionHandlingMode.IMMEDIATE_ACQUISITION_AND_HOLD);
    JdbcObserver jdbcObserver = Mockito.mock(JdbcObserver.class);
    when(sessionContext.getObserver()).thenReturn(jdbcObserver);
    JdbcServices jdbcServices = Mockito.mock(JdbcServices.class);
    when(serviceRegistry.getService(eq(JdbcServices.class))).thenReturn(jdbcServices);
    SqlExceptionHelper sqlExceptionHelper = Mockito.mock(SqlExceptionHelper.class);
    when(jdbcServices.getSqlExceptionHelper()).thenReturn(sqlExceptionHelper);
    JdbcCoordinatorImpl jdbcCoordinator = new JdbcCoordinatorImpl(null, sessionOwner);
    Batch currentBatch = Mockito.mock(Batch.class);
    Field currentBatchField = JdbcCoordinatorImpl.class.getDeclaredField("currentBatch");
    currentBatchField.setAccessible(true);
    currentBatchField.set(jdbcCoordinator, currentBatch);
    doThrow(IllegalStateException.class).when(currentBatch).release();
    try {
        jdbcCoordinator.close();
        fail("Should throw IllegalStateException");
    } catch (Exception expected) {
        assertEquals(IllegalStateException.class, expected.getClass());
    }
    verify(jdbcConnectionAccess, times(1)).releaseConnection(same(connection));
}
Also used : JdbcSessionContext(org.hibernate.resource.jdbc.spi.JdbcSessionContext) Connection(java.sql.Connection) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) SqlExceptionHelper(org.hibernate.engine.jdbc.spi.SqlExceptionHelper) JdbcSessionOwner(org.hibernate.resource.jdbc.spi.JdbcSessionOwner) SQLException(java.sql.SQLException) Field(java.lang.reflect.Field) JdbcObserver(org.hibernate.resource.jdbc.spi.JdbcObserver) Batch(org.hibernate.engine.jdbc.batch.spi.Batch) JdbcConnectionAccess(org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess) ServiceRegistry(org.hibernate.service.ServiceRegistry) Test(org.junit.Test)

Example 2 with Batch

use of org.hibernate.engine.jdbc.batch.spi.Batch in project hibernate-orm by hibernate.

the class BatchingTest method testSessionBatchingUsage.

@Test
public void testSessionBatchingUsage() throws Exception {
    Session session = openSession();
    session.setJdbcBatchSize(3);
    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(0, batchObserver.getImplicitExecutionCount());
    assertTrue(jdbcCoordinator.getResourceRegistry().hasRegisteredResources());
    PreparedStatement insert3 = insertBatch.getBatchStatement(insertSql, false);
    assertSame(insert, insert3);
    insert = insert3;
    insert.setLong(1, 3);
    insert.setString(2, "yet 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();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) LogicalConnectionImplementor(org.hibernate.resource.jdbc.spi.LogicalConnectionImplementor) BatchBuilder(org.hibernate.engine.jdbc.batch.spi.BatchBuilder) PreparedStatement(java.sql.PreparedStatement) JdbcCoordinator(org.hibernate.engine.jdbc.spi.JdbcCoordinator) JournalingBatchObserver(org.hibernate.test.common.JournalingBatchObserver) Transaction(org.hibernate.Transaction) NonBatchingBatch(org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch) Batch(org.hibernate.engine.jdbc.batch.spi.Batch) BatchingBatch(org.hibernate.engine.jdbc.batch.internal.BatchingBatch) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) BasicBatchKey(org.hibernate.engine.jdbc.batch.internal.BasicBatchKey) BatchKey(org.hibernate.engine.jdbc.batch.spi.BatchKey) BasicBatchKey(org.hibernate.engine.jdbc.batch.internal.BasicBatchKey) NonBatchingBatch(org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch) BatchingBatch(org.hibernate.engine.jdbc.batch.internal.BatchingBatch) BatchBuilderImpl(org.hibernate.engine.jdbc.batch.internal.BatchBuilderImpl) Session(org.hibernate.Session) Test(org.junit.Test)

Example 3 with Batch

use of org.hibernate.engine.jdbc.batch.spi.Batch in project hibernate-orm by hibernate.

the class BatchingBatchFailureTest 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"));
        session.persist(new User(4, "ok"));
        session.persist(new User(5, "ok"));
        session.persist(new User(6, "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(BatchingBatch.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();
    }
}
Also used : Field(java.lang.reflect.Field) Batch(org.hibernate.engine.jdbc.batch.spi.Batch) BatchingBatch(org.hibernate.engine.jdbc.batch.internal.BatchingBatch) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) BatchingBatch(org.hibernate.engine.jdbc.batch.internal.BatchingBatch) Map(java.util.Map) Session(org.hibernate.Session) Test(org.junit.Test)

Example 4 with Batch

use of org.hibernate.engine.jdbc.batch.spi.Batch 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();
    }
}
Also used : Field(java.lang.reflect.Field) NonBatchingBatch(org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch) Batch(org.hibernate.engine.jdbc.batch.spi.Batch) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) NonBatchingBatch(org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch) Map(java.util.Map) Session(org.hibernate.Session) Test(org.junit.Test)

Example 5 with Batch

use of org.hibernate.engine.jdbc.batch.spi.Batch 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();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) LogicalConnectionImplementor(org.hibernate.resource.jdbc.spi.LogicalConnectionImplementor) BatchBuilder(org.hibernate.engine.jdbc.batch.spi.BatchBuilder) PreparedStatement(java.sql.PreparedStatement) NonBatchingBatch(org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch) JdbcCoordinator(org.hibernate.engine.jdbc.spi.JdbcCoordinator) JournalingBatchObserver(org.hibernate.test.common.JournalingBatchObserver) Transaction(org.hibernate.Transaction) NonBatchingBatch(org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch) Batch(org.hibernate.engine.jdbc.batch.spi.Batch) BatchingBatch(org.hibernate.engine.jdbc.batch.internal.BatchingBatch) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) BasicBatchKey(org.hibernate.engine.jdbc.batch.internal.BasicBatchKey) BatchKey(org.hibernate.engine.jdbc.batch.spi.BatchKey) BasicBatchKey(org.hibernate.engine.jdbc.batch.internal.BasicBatchKey) BatchBuilderImpl(org.hibernate.engine.jdbc.batch.internal.BatchBuilderImpl) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

Batch (org.hibernate.engine.jdbc.batch.spi.Batch)6 Test (org.junit.Test)6 Session (org.hibernate.Session)5 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)5 BatchingBatch (org.hibernate.engine.jdbc.batch.internal.BatchingBatch)4 NonBatchingBatch (org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch)4 Field (java.lang.reflect.Field)3 PreparedStatement (java.sql.PreparedStatement)3 Statement (java.sql.Statement)3 Transaction (org.hibernate.Transaction)3 BasicBatchKey (org.hibernate.engine.jdbc.batch.internal.BasicBatchKey)3 BatchBuilderImpl (org.hibernate.engine.jdbc.batch.internal.BatchBuilderImpl)3 BatchBuilder (org.hibernate.engine.jdbc.batch.spi.BatchBuilder)3 BatchKey (org.hibernate.engine.jdbc.batch.spi.BatchKey)3 JdbcCoordinator (org.hibernate.engine.jdbc.spi.JdbcCoordinator)3 LogicalConnectionImplementor (org.hibernate.resource.jdbc.spi.LogicalConnectionImplementor)3 JournalingBatchObserver (org.hibernate.test.common.JournalingBatchObserver)3 Map (java.util.Map)2 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1