Search in sources :

Example 11 with JdbcConnectionAccess

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

the class JpaTckUsageTest method dropProcedures.

private void dropProcedures(HibernateEntityManagerFactory emf) {
    final SessionFactoryImplementor sf = emf.unwrap(SessionFactoryImplementor.class);
    final JdbcConnectionAccess connectionAccess = sf.getServiceRegistry().getService(JdbcServices.class).getBootstrapJdbcConnectionAccess();
    final Connection conn;
    try {
        conn = connectionAccess.obtainConnection();
        conn.setAutoCommit(false);
        try {
            Statement statement = conn.createStatement();
            dropProcedures(statement);
            try {
                statement.close();
            } catch (SQLException ignore) {
            }
        } finally {
            try {
                conn.commit();
            } catch (SQLException e) {
                System.out.println("Unable to commit transaction afterQuery creating dropping procedures");
            }
            try {
                connectionAccess.releaseConnection(conn);
            } catch (SQLException ignore) {
            }
        }
    } catch (SQLException e) {
        throw new RuntimeException("Unable to drop stored procedures", e);
    }
}
Also used : SQLException(java.sql.SQLException) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) JdbcConnectionAccess(org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess) Connection(java.sql.Connection) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices)

Example 12 with JdbcConnectionAccess

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

the class StoreProcedureOutParameterByNameTest method createProcedure.

private void createProcedure(EntityManagerFactory emf, String storedProc) {
    final SessionFactoryImplementor sf = emf.unwrap(SessionFactoryImplementor.class);
    final JdbcConnectionAccess connectionAccess = sf.getServiceRegistry().getService(JdbcServices.class).getBootstrapJdbcConnectionAccess();
    final Connection conn;
    try {
        conn = connectionAccess.obtainConnection();
        conn.setAutoCommit(false);
        try {
            Statement statement = conn.createStatement();
            statement.execute(storedProc);
            try {
                statement.close();
            } catch (SQLException ignore) {
                fail();
            }
        } finally {
            try {
                conn.commit();
            } catch (SQLException e) {
                System.out.println("Unable to commit transaction after creating creating procedures");
                fail();
            }
            try {
                connectionAccess.releaseConnection(conn);
            } catch (SQLException ignore) {
                fail();
            }
        }
    } catch (SQLException e) {
        throw new RuntimeException("Unable to create stored procedures", e);
    }
}
Also used : SQLException(java.sql.SQLException) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) Statement(java.sql.Statement) JdbcConnectionAccess(org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess) Connection(java.sql.Connection) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices)

Example 13 with JdbcConnectionAccess

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

the class DateTimeParameterTest method createProcedures.

private void createProcedures(HibernateEntityManagerFactory emf) {
    final SessionFactoryImplementor sf = emf.unwrap(SessionFactoryImplementor.class);
    final JdbcConnectionAccess connectionAccess = sf.getServiceRegistry().getService(JdbcServices.class).getBootstrapJdbcConnectionAccess();
    final Connection conn;
    try {
        conn = connectionAccess.obtainConnection();
        conn.setAutoCommit(false);
        try {
            Statement statement = conn.createStatement();
            // drop them, just to be sure
            try {
                dropProcedures(statement);
            } catch (SQLException ignore) {
            }
            statement.execute("CREATE PROCEDURE findMessagesByDate(IN chkDt DATE) " + "language java " + "dynamic result sets 1 " + "external name 'org.hibernate.jpa.test.procedure.DateTimeParameterTest.findMessagesByDate' " + "parameter style java");
            statement.execute("CREATE PROCEDURE findMessagesByTime(IN chkTime TIME) " + "language java " + "dynamic result sets 1 " + "external name 'org.hibernate.jpa.test.procedure.DateTimeParameterTest.findMessagesByTime' " + "parameter style java");
            statement.execute("CREATE PROCEDURE findMessagesByTimestampRange(IN startDt TIMESTAMP, IN endDt TIMESTAMP) " + "language java " + "dynamic result sets 1 " + "external name 'org.hibernate.jpa.test.procedure.DateTimeParameterTest.findMessagesByTimestampRange' " + "parameter style java");
            statement.execute("CREATE PROCEDURE retrieveTimestamp(IN ts1 TIMESTAMP, OUT ts2 TIMESTAMP) " + "language java " + "dynamic result sets 0 " + "external name 'org.hibernate.jpa.test.procedure.DateTimeParameterTest.retrieveTimestamp' " + "parameter style java");
            try {
                statement.close();
            } catch (SQLException ignore) {
            }
        } finally {
            try {
                conn.commit();
            } catch (SQLException e) {
                System.out.println("Unable to commit transaction afterQuery creating creating procedures");
            }
            try {
                connectionAccess.releaseConnection(conn);
            } catch (SQLException ignore) {
            }
        }
    } catch (SQLException e) {
        throw new RuntimeException("Unable to create stored procedures", e);
    }
}
Also used : SQLException(java.sql.SQLException) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) JdbcConnectionAccess(org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess) Connection(java.sql.Connection) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices)

Example 14 with JdbcConnectionAccess

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

the class HSQLStoreProcedureTest method executeStatement.

public void executeStatement(EntityManagerFactory emf, String toExecute) {
    final SessionFactoryImplementor sf = emf.unwrap(SessionFactoryImplementor.class);
    final JdbcConnectionAccess connectionAccess = sf.getServiceRegistry().getService(JdbcServices.class).getBootstrapJdbcConnectionAccess();
    final Connection conn;
    try {
        conn = connectionAccess.obtainConnection();
        conn.setAutoCommit(false);
        try {
            Statement statement = conn.createStatement();
            statement.execute(toExecute);
            try {
                statement.close();
            } catch (SQLException e) {
                fail(e.getMessage());
            }
        } finally {
            try {
                conn.commit();
            } catch (SQLException e) {
                fail(e.getMessage());
            }
            try {
                connectionAccess.releaseConnection(conn);
            } catch (SQLException e) {
                fail(e.getMessage());
            }
        }
    } catch (SQLException e) {
        throw new RuntimeException("Unable to create stored procedures", e);
    }
}
Also used : SQLException(java.sql.SQLException) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) Statement(java.sql.Statement) JdbcConnectionAccess(org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess) Connection(java.sql.Connection) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices)

Example 15 with JdbcConnectionAccess

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

the class StoreProcedureOutParameterByPositionTest method createProcedure.

private void createProcedure(EntityManagerFactory emf, String storedProc) {
    final SessionFactoryImplementor sf = emf.unwrap(SessionFactoryImplementor.class);
    final JdbcConnectionAccess connectionAccess = sf.getServiceRegistry().getService(JdbcServices.class).getBootstrapJdbcConnectionAccess();
    final Connection conn;
    try {
        conn = connectionAccess.obtainConnection();
        conn.setAutoCommit(false);
        try {
            Statement statement = conn.createStatement();
            statement.execute(storedProc);
            try {
                statement.close();
            } catch (SQLException ignore) {
                fail();
            }
        } finally {
            try {
                conn.commit();
            } catch (SQLException e) {
                System.out.println("Unable to commit transaction after creating creating procedures");
                fail();
            }
            try {
                connectionAccess.releaseConnection(conn);
            } catch (SQLException ignore) {
                fail();
            }
        }
    } catch (SQLException e) {
        throw new RuntimeException("Unable to create stored procedures", e);
    }
}
Also used : SQLException(java.sql.SQLException) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) Statement(java.sql.Statement) JdbcConnectionAccess(org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess) Connection(java.sql.Connection) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices)

Aggregations

Connection (java.sql.Connection)15 JdbcConnectionAccess (org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess)15 SQLException (java.sql.SQLException)14 JdbcServices (org.hibernate.engine.jdbc.spi.JdbcServices)13 Statement (java.sql.Statement)10 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)10 PreparedStatement (java.sql.PreparedStatement)4 SqlExceptionHelper (org.hibernate.engine.jdbc.spi.SqlExceptionHelper)3 JdbcSessionContext (org.hibernate.resource.jdbc.spi.JdbcSessionContext)2 JdbcSessionOwner (org.hibernate.resource.jdbc.spi.JdbcSessionOwner)2 ServiceRegistry (org.hibernate.service.ServiceRegistry)2 Test (org.junit.Test)2 Field (java.lang.reflect.Field)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeUnit (java.util.concurrent.TimeUnit)1 Predicate (java.util.function.Predicate)1