Skip to content

Commit

Permalink
Test warnings when executing StoredProcedureQuery (quarkusio#13273)
Browse files Browse the repository at this point in the history
Signed-off-by: Yoann Rodière <yoann@hibernate.org>
  • Loading branch information
yrodiere committed Feb 5, 2021
1 parent 9834614 commit aaf9ada
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -10,20 +13,26 @@
import java.util.logging.Level;

import javax.persistence.EntityManager;
import javax.persistence.ParameterMode;
import javax.persistence.StoredProcedureQuery;

import org.hibernate.BaseSessionEventListener;
import org.hibernate.Session;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.agroal.api.AgroalDataSource;
import io.quarkus.arc.Arc;
import io.quarkus.test.QuarkusUnitTest;

/**
* Check transaction lifecycle, including session flushes and the closing of the session.
* Check transaction lifecycle, including session flushes, the closing of the session,
* and the release of JDBC resources.
*/
public abstract class AbstractTransactionLifecycleTest {

Expand All @@ -39,6 +48,17 @@ public abstract class AbstractTransactionLifecycleTest {
.setLogRecordPredicate(record -> record.getLevel().intValue() >= Level.WARNING.intValue())
.assertLogRecords(records -> assertThat(records).isEmpty());

@BeforeAll
public static void installStoredProcedure() throws SQLException {
AgroalDataSource dataSource = Arc.container().instance(AgroalDataSource.class).get();
try (Connection conn = dataSource.getConnection()) {
try (Statement st = conn.createStatement()) {
st.execute("CREATE ALIAS " + MyStoredProcedure.NAME
+ " FOR \"" + MyStoredProcedure.class.getName() + ".execute\"");
}
}
}

@Test
public void testLifecycle() {
long id = 1L;
Expand Down Expand Up @@ -73,6 +93,13 @@ public void testLifecycle() {
LifecycleOperation.TRANSACTION_COMPLETION);
assertThat(retrieved.value).isEqualTo(UPDATED_NAME);

// See https://github.com/quarkusio/quarkus/issues/13273
ValueAndExecutionMetadata<String> calledStoredProcedure = crud.callStoredProcedure(id);
checkPostConditions(calledStoredProcedure,
// Strangely, calling a stored procedure isn't considered as a statement for Hibernate ORM listeners
LifecycleOperation.TRANSACTION_COMPLETION);
assertThat(calledStoredProcedure.value).isEqualTo(MyStoredProcedure.execute(id));

ValueAndExecutionMetadata<Void> deleted = crud.delete(id);
checkPostConditions(deleted,
LifecycleOperation.STATEMENT, // select
Expand Down Expand Up @@ -119,6 +146,16 @@ public ValueAndExecutionMetadata<String> retrieve(long id) {
});
}

public ValueAndExecutionMetadata<String> callStoredProcedure(long id) {
return inTransaction(entityManager -> {
StoredProcedureQuery storedProcedure = entityManager.createStoredProcedureQuery(MyStoredProcedure.NAME);
storedProcedure.registerStoredProcedureParameter(0, Long.class, ParameterMode.IN);
storedProcedure.setParameter(0, id);
storedProcedure.execute();
return (String) storedProcedure.getSingleResult();
});
}

public ValueAndExecutionMetadata<Void> update(long id, String name) {
return inTransaction(entityManager -> {
SimpleEntity entity = entityManager.find(SimpleEntity.class, id);
Expand Down Expand Up @@ -183,4 +220,14 @@ private enum LifecycleOperation {
FLUSH,
TRANSACTION_COMPLETION;
}

public static class MyStoredProcedure {
private static final String NAME = "myStoredProc";
private static final String RESULT_PREFIX = "StoredProcResult";

@SuppressWarnings("unused")
public static String execute(long id) {
return RESULT_PREFIX + id;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ protected TestCRUD crud() {

@Override
protected boolean expectDoubleFlush() {
// FIXME: We expect double flushes in this case, but that's a bug
return true;
return false;
}

@ApplicationScoped
Expand All @@ -43,6 +42,12 @@ public ValueAndExecutionMetadata<String> retrieve(long id) {
return super.retrieve(id);
}

@Override
@Transactional
public ValueAndExecutionMetadata<String> callStoredProcedure(long id) {
return super.callStoredProcedure(id);
}

@Override
@Transactional
public ValueAndExecutionMetadata<Void> update(long id, String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ protected TestCRUD crud() {

@Override
protected boolean expectDoubleFlush() {
// FIXME: We expect double flushes in this case, but that's a bug
return true;
return false;
}

@ApplicationScoped
Expand Down

0 comments on commit aaf9ada

Please sign in to comment.