Search in sources :

Example 1 with JdbcCoordinator

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

the class SQLExceptionConversionTest method testNotNullConstraint.

@Test
@TestForIssue(jiraKey = "HHH-7357")
public void testNotNullConstraint() {
    final Session session = openSession();
    session.beginTransaction();
    final User user = new User();
    user.setUsername("Lukasz");
    session.save(user);
    session.flush();
    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 ps = null;
            try {
                ps = statementPreparer.prepareStatement("UPDATE T_USER SET user_name = ? WHERE user_id = ?");
                // Attempt to update user name to NULL (NOT NULL constraint defined).
                ps.setNull(1, Types.VARCHAR);
                ps.setLong(2, user.getId());
                resultSetReturn.executeUpdate(ps);
                fail("UPDATE should have failed because of not NULL constraint.");
            } catch (ConstraintViolationException ignore) {
            // expected outcome
            } finally {
                releaseStatement(session, ps);
            }
        }
    });
    session.getTransaction().rollback();
    session.close();
}
Also used : ResultSetReturn(org.hibernate.engine.jdbc.spi.ResultSetReturn) JdbcCoordinator(org.hibernate.engine.jdbc.spi.JdbcCoordinator) SQLException(java.sql.SQLException) Work(org.hibernate.jdbc.Work) Connection(java.sql.Connection) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) PreparedStatement(java.sql.PreparedStatement) StatementPreparer(org.hibernate.engine.jdbc.spi.StatementPreparer) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 2 with JdbcCoordinator

use of org.hibernate.engine.jdbc.spi.JdbcCoordinator 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 JdbcCoordinator

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

the class CursorFromCallableTest method testStatementClosing.

@Test
@TestForIssue(jiraKey = "HHH-7984")
public void testStatementClosing() {
    Session session = openSession();
    session.getTransaction().begin();
    // Reading maximum number of opened cursors requires SYS privileges.
    // Verify statement closing with JdbcCoordinator#hasRegisteredResources() instead.
    // BigDecimal maxCursors = (BigDecimal) session.createSQLQuery( "SELECT value FROM v$parameter WHERE name = 'open_cursors'" ).uniqueResult();
    // for ( int i = 0; i < maxCursors + 10; ++i ) { named_query_execution }
    Assert.assertEquals(Arrays.asList(new NumValue(1, "Line 1"), new NumValue(2, "Line 2")), session.getNamedQuery("NumValue.getSomeValues").list());
    JdbcCoordinator jdbcCoordinator = ((SessionImplementor) session).getJdbcCoordinator();
    Assert.assertFalse("Prepared statement and result set should be released after query execution.", jdbcCoordinator.getResourceRegistry().hasRegisteredResources());
    session.getTransaction().commit();
    session.close();
}
Also used : JdbcCoordinator(org.hibernate.engine.jdbc.spi.JdbcCoordinator) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 4 with JdbcCoordinator

use of org.hibernate.engine.jdbc.spi.JdbcCoordinator 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 5 with JdbcCoordinator

use of org.hibernate.engine.jdbc.spi.JdbcCoordinator 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());
}
Also used : JdbcCoordinator(org.hibernate.engine.jdbc.spi.JdbcCoordinator) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException) JDBCException(org.hibernate.JDBCException) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

Session (org.hibernate.Session)7 JdbcCoordinator (org.hibernate.engine.jdbc.spi.JdbcCoordinator)7 PreparedStatement (java.sql.PreparedStatement)6 Test (org.junit.Test)6 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)5 Statement (java.sql.Statement)4 SQLException (java.sql.SQLException)3 Transaction (org.hibernate.Transaction)3 BasicBatchKey (org.hibernate.engine.jdbc.batch.internal.BasicBatchKey)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 LogicalConnectionImplementor (org.hibernate.resource.jdbc.spi.LogicalConnectionImplementor)3 JournalingBatchObserver (org.hibernate.test.common.JournalingBatchObserver)3 Connection (java.sql.Connection)2 ResultSetReturn (org.hibernate.engine.jdbc.spi.ResultSetReturn)2 StatementPreparer (org.hibernate.engine.jdbc.spi.StatementPreparer)2