HHH-13144 - Move the doInAutoCommit utility to TranscationUtil
This commit is contained in:
parent
3ff3615d00
commit
d986ae92d0
|
@ -1,63 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.test.procedure;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.ParameterMode;
|
||||
import javax.persistence.StoredProcedureQuery;
|
||||
|
||||
import org.hibernate.boot.registry.StandardServiceRegistry;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.dialect.SQLServer2012Dialect;
|
||||
import org.hibernate.engine.jdbc.spi.JdbcServices;
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
|
||||
import org.hibernate.testing.RequiresDialect;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
public abstract class AbstractStoredProcedureTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
protected void doInAutoCommit(Consumer<Statement> consumer, Map settings) {
|
||||
StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder();
|
||||
if ( settings != null ) {
|
||||
ssrb.applySettings( settings );
|
||||
}
|
||||
StandardServiceRegistry ssr = ssrb.build();
|
||||
|
||||
try {
|
||||
try (Connection connection = ssr.getService( JdbcServices.class )
|
||||
.getBootstrapJdbcConnectionAccess()
|
||||
.obtainConnection();
|
||||
Statement statement = connection.createStatement()) {
|
||||
connection.setAutoCommit( true );
|
||||
consumer.accept( statement );
|
||||
}
|
||||
catch (SQLException e) {
|
||||
log.debug( e.getMessage() );
|
||||
}
|
||||
}
|
||||
finally {
|
||||
StandardServiceRegistryBuilder.destroy( ssr );
|
||||
}
|
||||
}
|
||||
|
||||
protected void doInAutoCommit(Consumer<Statement> consumer) {
|
||||
doInAutoCommit( consumer, null );
|
||||
}
|
||||
}
|
|
@ -34,6 +34,7 @@ import org.hibernate.testing.TestForIssue;
|
|||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInAutoCommit;
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
|
@ -42,7 +43,7 @@ import static org.junit.Assert.assertEquals;
|
|||
*/
|
||||
@RequiresDialect(SQLServer2012Dialect.class)
|
||||
@TestForIssue( jiraKey = "HHH-12704" )
|
||||
public class SQLServerStoredProcedureCrossDatabaseTest extends AbstractStoredProcedureTest {
|
||||
public class SQLServerStoredProcedureCrossDatabaseTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
private final String DATABASE_NAME_TOKEN = "databaseName=";
|
||||
|
||||
|
@ -59,23 +60,10 @@ public class SQLServerStoredProcedureCrossDatabaseTest extends AbstractStoredPro
|
|||
|
||||
@Before
|
||||
public void init() {
|
||||
doInAutoCommit( statement -> {
|
||||
try {
|
||||
statement.executeUpdate( "DROP DATABASE " + DATABASE_NAME );
|
||||
}
|
||||
catch (SQLException e) {
|
||||
log.debug( e.getMessage() );
|
||||
}
|
||||
} );
|
||||
|
||||
doInAutoCommit( statement -> {
|
||||
try {
|
||||
statement.executeUpdate( "CREATE DATABASE " + DATABASE_NAME );
|
||||
}
|
||||
catch (SQLException e) {
|
||||
log.debug( e.getMessage() );
|
||||
}
|
||||
} );
|
||||
doInAutoCommit(
|
||||
"DROP DATABASE " + DATABASE_NAME,
|
||||
"CREATE DATABASE " + DATABASE_NAME
|
||||
);
|
||||
|
||||
String url = (String) Environment.getProperties().get( AvailableSettings.URL );
|
||||
|
||||
|
@ -83,31 +71,16 @@ public class SQLServerStoredProcedureCrossDatabaseTest extends AbstractStoredPro
|
|||
|
||||
url = tokens[0] + DATABASE_NAME_TOKEN + DATABASE_NAME;
|
||||
|
||||
doInAutoCommit( statement -> {
|
||||
try {
|
||||
statement.executeUpdate( "DROP PROCEDURE sp_square_number" );
|
||||
}
|
||||
catch (SQLException e) {
|
||||
log.debug( e.getMessage() );
|
||||
}
|
||||
}, Collections.singletonMap( AvailableSettings.URL, url ));
|
||||
|
||||
doInAutoCommit( statement -> {
|
||||
try {
|
||||
statement.executeUpdate(
|
||||
"CREATE PROCEDURE sp_square_number " +
|
||||
" @inputNumber INT, " +
|
||||
" @outputNumber INT OUTPUT " +
|
||||
"AS " +
|
||||
"BEGIN " +
|
||||
" SELECT @outputNumber = @inputNumber * @inputNumber; " +
|
||||
"END"
|
||||
);
|
||||
}
|
||||
catch (SQLException e) {
|
||||
log.debug( e.getMessage() );
|
||||
}
|
||||
}, Collections.singletonMap( AvailableSettings.URL, url ));
|
||||
doInAutoCommit( Collections.singletonMap( AvailableSettings.URL, url ),
|
||||
"DROP PROCEDURE sp_square_number",
|
||||
"CREATE PROCEDURE sp_square_number " +
|
||||
" @inputNumber INT, " +
|
||||
" @outputNumber INT OUTPUT " +
|
||||
"AS " +
|
||||
"BEGIN " +
|
||||
" SELECT @outputNumber = @inputNumber * @inputNumber; " +
|
||||
"END"
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -11,11 +11,13 @@ import javax.persistence.ParameterMode;
|
|||
import javax.persistence.StoredProcedureQuery;
|
||||
|
||||
import org.hibernate.dialect.SQLServer2012Dialect;
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
|
||||
import org.hibernate.testing.RequiresDialect;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInAutoCommit;
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
|
@ -23,7 +25,7 @@ import static org.junit.Assert.assertEquals;
|
|||
* @author Vlad Mihalcea
|
||||
*/
|
||||
@RequiresDialect(SQLServer2012Dialect.class)
|
||||
public class SQLServerStoredProcedureCrossSchemaTest extends AbstractStoredProcedureTest {
|
||||
public class SQLServerStoredProcedureCrossSchemaTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
|
@ -35,49 +37,18 @@ public class SQLServerStoredProcedureCrossSchemaTest extends AbstractStoredProce
|
|||
|
||||
@Before
|
||||
public void init() {
|
||||
doInAutoCommit( statement -> {
|
||||
try {
|
||||
statement.executeUpdate( "DROP PROCEDURE sp_test.sp_square_number" );
|
||||
}
|
||||
catch (SQLException e) {
|
||||
log.debug( e.getMessage() );
|
||||
}
|
||||
} );
|
||||
|
||||
doInAutoCommit( statement -> {
|
||||
try {
|
||||
statement.executeUpdate( "DROP SCHEMA sp_test" );
|
||||
}
|
||||
catch (SQLException e) {
|
||||
log.debug( e.getMessage() );
|
||||
}
|
||||
} );
|
||||
|
||||
doInAutoCommit( statement -> {
|
||||
try {
|
||||
statement.executeUpdate( "CREATE SCHEMA sp_test" );
|
||||
}
|
||||
catch (SQLException e) {
|
||||
log.debug( e.getMessage() );
|
||||
}
|
||||
} );
|
||||
|
||||
doInAutoCommit( statement -> {
|
||||
try {
|
||||
statement.executeUpdate(
|
||||
"CREATE PROCEDURE sp_test.sp_square_number " +
|
||||
" @inputNumber INT, " +
|
||||
" @outputNumber INT OUTPUT " +
|
||||
"AS " +
|
||||
"BEGIN " +
|
||||
" SELECT @outputNumber = @inputNumber * @inputNumber; " +
|
||||
"END"
|
||||
);
|
||||
}
|
||||
catch (SQLException e) {
|
||||
log.debug( e.getMessage() );
|
||||
}
|
||||
} );
|
||||
doInAutoCommit(
|
||||
"DROP PROCEDURE sp_test.sp_square_number",
|
||||
"DROP SCHEMA sp_test",
|
||||
"CREATE SCHEMA sp_test",
|
||||
"CREATE PROCEDURE sp_test.sp_square_number " +
|
||||
" @inputNumber INT, " +
|
||||
" @outputNumber INT OUTPUT " +
|
||||
"AS " +
|
||||
"BEGIN " +
|
||||
" SELECT @outputNumber = @inputNumber * @inputNumber; " +
|
||||
"END"
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -29,6 +29,7 @@ import org.hibernate.testing.RequiresDialect;
|
|||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInAutoCommit;
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
@ -38,7 +39,7 @@ import static org.junit.Assert.assertTrue;
|
|||
* @author Vlad Mihalcea
|
||||
*/
|
||||
@RequiresDialect(SQLServer2012Dialect.class)
|
||||
public class SQLServerStoredProcedureTest extends AbstractStoredProcedureTest {
|
||||
public class SQLServerStoredProcedureTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
|
@ -50,78 +51,41 @@ public class SQLServerStoredProcedureTest extends AbstractStoredProcedureTest {
|
|||
|
||||
@Before
|
||||
public void init() {
|
||||
doInAutoCommit( statement -> {
|
||||
try {
|
||||
statement.executeUpdate( "DROP PROCEDURE sp_count_phones" );
|
||||
}
|
||||
catch (SQLException e) {
|
||||
log.debug( e.getMessage() );
|
||||
}
|
||||
} );
|
||||
|
||||
doInAutoCommit( statement -> {
|
||||
try {
|
||||
statement.executeUpdate( "DROP FUNCTION fn_count_phones" );
|
||||
}
|
||||
catch (SQLException e) {
|
||||
log.debug( e.getMessage() );
|
||||
}
|
||||
} );
|
||||
|
||||
doInAutoCommit( statement -> {
|
||||
try {
|
||||
statement.executeUpdate( "DROP PROCEDURE sp_phones" );
|
||||
}
|
||||
catch (SQLException e) {
|
||||
log.debug( e.getMessage() );
|
||||
}
|
||||
} );
|
||||
|
||||
doInAutoCommit( statement -> {
|
||||
try {
|
||||
statement.executeUpdate(
|
||||
"CREATE PROCEDURE sp_count_phones " +
|
||||
" @personId INT, " +
|
||||
" @phoneCount INT OUTPUT " +
|
||||
"AS " +
|
||||
"BEGIN " +
|
||||
" SELECT @phoneCount = COUNT(*) " +
|
||||
" FROM Phone " +
|
||||
" WHERE person_id = @personId " +
|
||||
"END"
|
||||
);
|
||||
|
||||
statement.executeUpdate(
|
||||
"CREATE FUNCTION fn_count_phones (@personId INT) " +
|
||||
"RETURNS INT " +
|
||||
"AS " +
|
||||
"BEGIN " +
|
||||
" DECLARE @phoneCount int; " +
|
||||
" SELECT @phoneCount = COUNT(*) " +
|
||||
" FROM Phone " +
|
||||
" WHERE person_id = @personId; " +
|
||||
" RETURN(@phoneCount); " +
|
||||
"END"
|
||||
);
|
||||
|
||||
statement.executeUpdate(
|
||||
"CREATE PROCEDURE sp_phones " +
|
||||
" @personId INT, " +
|
||||
" @phones CURSOR VARYING OUTPUT " +
|
||||
"AS " +
|
||||
" SET NOCOUNT ON; " +
|
||||
" SET @phones = CURSOR " +
|
||||
" FORWARD_ONLY STATIC FOR " +
|
||||
" SELECT * " +
|
||||
" FROM Phone " +
|
||||
" WHERE person_id = @personId; " +
|
||||
" OPEN @phones;"
|
||||
);
|
||||
}
|
||||
catch (SQLException e) {
|
||||
log.debug( e.getMessage() );
|
||||
}
|
||||
} );
|
||||
doInAutoCommit(
|
||||
"DROP PROCEDURE sp_count_phones",
|
||||
"DROP FUNCTION fn_count_phones",
|
||||
"DROP PROCEDURE sp_phones",
|
||||
"CREATE PROCEDURE sp_count_phones " +
|
||||
" @personId INT, " +
|
||||
" @phoneCount INT OUTPUT " +
|
||||
"AS " +
|
||||
"BEGIN " +
|
||||
" SELECT @phoneCount = COUNT(*) " +
|
||||
" FROM Phone " +
|
||||
" WHERE person_id = @personId " +
|
||||
"END",
|
||||
"CREATE FUNCTION fn_count_phones (@personId INT) " +
|
||||
"RETURNS INT " +
|
||||
"AS " +
|
||||
"BEGIN " +
|
||||
" DECLARE @phoneCount int; " +
|
||||
" SELECT @phoneCount = COUNT(*) " +
|
||||
" FROM Phone " +
|
||||
" WHERE person_id = @personId; " +
|
||||
" RETURN(@phoneCount); " +
|
||||
"END",
|
||||
"CREATE PROCEDURE sp_phones " +
|
||||
" @personId INT, " +
|
||||
" @phones CURSOR VARYING OUTPUT " +
|
||||
"AS " +
|
||||
" SET NOCOUNT ON; " +
|
||||
" SET @phones = CURSOR " +
|
||||
" FORWARD_ONLY STATIC FOR " +
|
||||
" SELECT * " +
|
||||
" FROM Phone " +
|
||||
" WHERE person_id = @personId; " +
|
||||
" OPEN @phones;"
|
||||
);
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person1 = new Person( "John Doe" );
|
||||
|
|
|
@ -6,7 +6,9 @@
|
|||
*/
|
||||
package org.hibernate.testing.transaction;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executors;
|
||||
|
@ -22,12 +24,15 @@ import org.hibernate.Session;
|
|||
import org.hibernate.SessionBuilder;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistry;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.dialect.AbstractHANADialect;
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.dialect.H2Dialect;
|
||||
import org.hibernate.dialect.MySQLDialect;
|
||||
import org.hibernate.dialect.PostgreSQL81Dialect;
|
||||
import org.hibernate.dialect.SQLServerDialect;
|
||||
import org.hibernate.engine.jdbc.spi.JdbcServices;
|
||||
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
|
@ -578,4 +583,82 @@ public class TransactionUtil {
|
|||
} );
|
||||
}
|
||||
|
||||
/**
|
||||
* Use the supplied settings for building a new {@link org.hibernate.service.ServiceRegistry} and
|
||||
* create a new JDBC {@link Connection} in auto-commit mode.
|
||||
*
|
||||
* A new JDBC {@link Statement} is created and passed to the supplied callback.
|
||||
*
|
||||
* @param consumer {@link Statement} callback to execute statements in auto-commit mode
|
||||
* @param settings Settings to build a new {@link org.hibernate.service.ServiceRegistry}
|
||||
*/
|
||||
public static void doInAutoCommit(Consumer<Statement> consumer, Map settings) {
|
||||
StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder();
|
||||
if ( settings != null ) {
|
||||
ssrb.applySettings( settings );
|
||||
}
|
||||
StandardServiceRegistry ssr = ssrb.build();
|
||||
|
||||
try {
|
||||
try (Connection connection = ssr.getService( JdbcServices.class )
|
||||
.getBootstrapJdbcConnectionAccess()
|
||||
.obtainConnection();
|
||||
Statement statement = connection.createStatement()) {
|
||||
connection.setAutoCommit( true );
|
||||
consumer.accept( statement );
|
||||
}
|
||||
catch (SQLException e) {
|
||||
log.debug( e.getMessage() );
|
||||
}
|
||||
}
|
||||
finally {
|
||||
StandardServiceRegistryBuilder.destroy( ssr );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Use the default settings for building a new {@link org.hibernate.service.ServiceRegistry} and
|
||||
* create a new JDBC {@link Connection} in auto-commit mode.
|
||||
*
|
||||
* A new JDBC {@link Statement} is created and passed to the supplied callback.
|
||||
*
|
||||
* @param consumer {@link Statement} callback to execute statements in auto-commit mode
|
||||
*/
|
||||
public static void doInAutoCommit(Consumer<Statement> consumer) {
|
||||
doInAutoCommit( consumer, null );
|
||||
}
|
||||
|
||||
/**
|
||||
* Use the supplied settings for building a new {@link org.hibernate.service.ServiceRegistry} and
|
||||
* create a new JDBC {@link Connection} in auto-commit mode.
|
||||
*
|
||||
* The supplied statements will be executed using the previously created connection
|
||||
*
|
||||
* @param settings Settings to build a new {@link org.hibernate.service.ServiceRegistry}
|
||||
* @param statements statements to be executed in auto-commit mode
|
||||
*/
|
||||
public static void doInAutoCommit(Map settings, String... statements) {
|
||||
doInAutoCommit( s -> {
|
||||
for ( String statement : statements ) {
|
||||
try {
|
||||
s.executeUpdate( statement );
|
||||
}
|
||||
catch (SQLException e) {
|
||||
log.debugf( e, "Statement [%s] execution failed!", statement );
|
||||
}
|
||||
}
|
||||
}, settings );
|
||||
}
|
||||
|
||||
/**
|
||||
* Use the default settings for building a new {@link org.hibernate.service.ServiceRegistry} and
|
||||
* create a new JDBC {@link Connection} in auto-commit mode.
|
||||
*
|
||||
* The supplied statements will be executed using the previously created connection
|
||||
*
|
||||
* @param statements statements to be executed in auto-commit mode
|
||||
*/
|
||||
public static void doInAutoCommit(String... statements) {
|
||||
doInAutoCommit( null, statements );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue