Search in sources :

Example 11 with BasicBatchKey

use of org.hibernate.engine.jdbc.batch.internal.BasicBatchKey in project hibernate-orm by hibernate.

the class AbstractCollectionPersister method deleteRows.

@Override
public void deleteRows(PersistentCollection collection, Serializable id, SharedSessionContractImplementor session) throws HibernateException {
    if (isInverse) {
        return;
    }
    if (!isRowDeleteEnabled()) {
        return;
    }
    if (LOG.isDebugEnabled()) {
        LOG.debugf("Deleting rows of collection: %s", MessageHelper.collectionInfoString(this, collection, id, session));
    }
    boolean deleteByIndex = !isOneToMany() && hasIndex && !indexContainsFormula;
    final Expectation expectation = Expectations.appropriateExpectation(getDeleteCheckStyle());
    try {
        // delete all the deleted entries
        Iterator deletes = collection.getDeletes(this, !deleteByIndex);
        if (deletes.hasNext()) {
            int offset = 1;
            int count = 0;
            while (deletes.hasNext()) {
                PreparedStatement st = null;
                boolean callable = isDeleteCallable();
                boolean useBatch = expectation.canBeBatched();
                String sql = getSQLDeleteRowString();
                if (useBatch) {
                    if (deleteBatchKey == null) {
                        deleteBatchKey = new BasicBatchKey(getRole() + "#DELETE", expectation);
                    }
                    st = session.getJdbcCoordinator().getBatch(deleteBatchKey).getBatchStatement(sql, callable);
                } else {
                    st = session.getJdbcCoordinator().getStatementPreparer().prepareStatement(sql, callable);
                }
                try {
                    expectation.prepare(st);
                    Object entry = deletes.next();
                    int loc = offset;
                    if (hasIdentifier) {
                        writeIdentifier(st, entry, loc, session);
                    } else {
                        loc = writeKey(st, id, loc, session);
                        if (deleteByIndex) {
                            writeIndexToWhere(st, entry, loc, session);
                        } else {
                            writeElementToWhere(st, entry, loc, session);
                        }
                    }
                    if (useBatch) {
                        session.getJdbcCoordinator().getBatch(deleteBatchKey).addToBatch();
                    } else {
                        expectation.verifyOutcome(session.getJdbcCoordinator().getResultSetReturn().executeUpdate(st), st, -1);
                    }
                    count++;
                } catch (SQLException sqle) {
                    if (useBatch) {
                        session.getJdbcCoordinator().abortBatch();
                    }
                    throw sqle;
                } finally {
                    if (!useBatch) {
                        session.getJdbcCoordinator().getResourceRegistry().release(st);
                        session.getJdbcCoordinator().afterStatementExecution();
                    }
                }
                LOG.debugf("Done deleting collection rows: %s deleted", count);
            }
        } else {
            LOG.debug("No rows to delete");
        }
    } catch (SQLException sqle) {
        throw sqlExceptionHelper.convert(sqle, "could not delete collection rows: " + MessageHelper.collectionInfoString(this, collection, id, session), getSQLDeleteRowString());
    }
}
Also used : SQLException(java.sql.SQLException) Iterator(java.util.Iterator) PreparedStatement(java.sql.PreparedStatement) BasicBatchKey(org.hibernate.engine.jdbc.batch.internal.BasicBatchKey) Expectation(org.hibernate.jdbc.Expectation)

Example 12 with BasicBatchKey

use of org.hibernate.engine.jdbc.batch.internal.BasicBatchKey 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)

Example 13 with BasicBatchKey

use of org.hibernate.engine.jdbc.batch.internal.BasicBatchKey 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();
}
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)

Aggregations

PreparedStatement (java.sql.PreparedStatement)13 BasicBatchKey (org.hibernate.engine.jdbc.batch.internal.BasicBatchKey)13 SQLException (java.sql.SQLException)10 Expectation (org.hibernate.jdbc.Expectation)10 Iterator (java.util.Iterator)5 Statement (java.sql.Statement)3 Session (org.hibernate.Session)3 Transaction (org.hibernate.Transaction)3 BatchBuilderImpl (org.hibernate.engine.jdbc.batch.internal.BatchBuilderImpl)3 BatchingBatch (org.hibernate.engine.jdbc.batch.internal.BatchingBatch)3 NonBatchingBatch (org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch)3 Batch (org.hibernate.engine.jdbc.batch.spi.Batch)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 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)3 LogicalConnectionImplementor (org.hibernate.resource.jdbc.spi.LogicalConnectionImplementor)3 JournalingBatchObserver (org.hibernate.test.common.JournalingBatchObserver)3 Test (org.junit.Test)3