use of org.hibernate.resource.jdbc.ResourceRegistry in project hibernate-orm by hibernate.
the class JpaStreamTest method runTerminalOperationTests.
private void runTerminalOperationTests(Runnable prepare, List<Runnable> onCloseCallbacks, Runnable onCloseAssertion, boolean flatMapBefore, boolean flatMapAfter, SessionFactoryScope scope) {
// collect as list
scope.inTransaction(session -> {
Stream<MyEntity> stream = getMyEntityStream(prepare, session, onCloseCallbacks, flatMapBefore, flatMapAfter);
ResourceRegistry resourceRegistry = resourceRegistry(session);
try {
List<MyEntity> entities = stream.collect(Collectors.toList());
assertTrue(resourceRegistry.hasRegisteredResources());
assertEquals(10, entities.size());
} finally {
stream.close();
assertFalse(resourceRegistry.hasRegisteredResources());
}
onCloseAssertion.run();
});
// forEach (TestCase based on attachment EntityManagerIllustrationTest.java in HHH-14449)
scope.inTransaction(session -> {
Stream<MyEntity> stream = getMyEntityStream(prepare, session, onCloseCallbacks, flatMapBefore, flatMapAfter);
ResourceRegistry resourceRegistry = resourceRegistry(session);
try {
AtomicInteger count = new AtomicInteger();
stream.forEach(myEntity -> count.incrementAndGet());
assertTrue(resourceRegistry.hasRegisteredResources());
assertEquals(10, count.get());
} finally {
stream.close();
assertFalse(resourceRegistry.hasRegisteredResources());
}
onCloseAssertion.run();
});
// filter (always true) + forEach (TestCase based on attachment EntityManagerIllustrationTest.java in HHH-14449)
scope.inTransaction(session -> {
Stream<MyEntity> stream = getMyEntityStream(prepare, session, onCloseCallbacks, flatMapBefore, flatMapAfter);
ResourceRegistry resourceRegistry = resourceRegistry(session);
try {
AtomicInteger count = new AtomicInteger();
stream.filter(Objects::nonNull).forEach(myEntity -> count.incrementAndGet());
assertTrue(resourceRegistry.hasRegisteredResources());
assertEquals(10, count.get());
} finally {
stream.close();
assertFalse(resourceRegistry.hasRegisteredResources());
}
onCloseAssertion.run();
});
// filter (partially true) + forEach (TestCase based on attachment EntityManagerIllustrationTest.java in HHH-14449)
scope.inTransaction(session -> {
Stream<MyEntity> stream = getMyEntityStream(prepare, session, onCloseCallbacks, flatMapBefore, flatMapAfter);
ResourceRegistry resourceRegistry = resourceRegistry(session);
try {
AtomicInteger count = new AtomicInteger();
stream.filter(entity -> entity.getId() % 2 == 0).forEach(myEntity -> count.incrementAndGet());
assertTrue(resourceRegistry.hasRegisteredResources());
assertEquals(5, count.get());
} finally {
stream.close();
assertFalse(resourceRegistry.hasRegisteredResources());
}
onCloseAssertion.run();
});
// multiple chained operations (TestCase based on attachment EntityManagerIllustrationTest.java in HHH-14449)
scope.inTransaction(session -> {
Stream<MyEntity> stream = getMyEntityStream(prepare, session, onCloseCallbacks, flatMapBefore, flatMapAfter);
ResourceRegistry resourceRegistry = resourceRegistry(session);
try {
AtomicInteger count = new AtomicInteger();
stream.filter(Objects::nonNull).map(Optional::of).filter(Optional::isPresent).map(Optional::get).forEach(myEntity -> count.incrementAndGet());
assertTrue(resourceRegistry.hasRegisteredResources());
assertEquals(10, count.get());
} finally {
stream.close();
assertFalse(resourceRegistry.hasRegisteredResources());
}
onCloseAssertion.run();
});
// mapToInt
scope.inTransaction(session -> {
Stream<MyEntity> stream = getMyEntityStream(prepare, session, onCloseCallbacks, flatMapBefore, flatMapAfter);
ResourceRegistry resourceRegistry = resourceRegistry(session);
try {
int sum = stream.mapToInt(MyEntity::getId).sum();
assertTrue(resourceRegistry.hasRegisteredResources());
assertEquals(55, sum);
} finally {
stream.close();
assertFalse(resourceRegistry.hasRegisteredResources());
}
onCloseAssertion.run();
});
// mapToLong
scope.inTransaction(session -> {
Stream<MyEntity> stream = getMyEntityStream(prepare, session, onCloseCallbacks, flatMapBefore, flatMapAfter);
ResourceRegistry resourceRegistry = resourceRegistry(session);
try {
long result = stream.mapToLong(entity -> entity.id * 10).min().getAsLong();
assertTrue(resourceRegistry.hasRegisteredResources());
assertEquals(10, result);
} finally {
stream.close();
assertFalse(resourceRegistry.hasRegisteredResources());
}
onCloseAssertion.run();
});
// mapToDouble
scope.inTransaction(session -> {
Stream<MyEntity> stream = getMyEntityStream(prepare, session, onCloseCallbacks, flatMapBefore, flatMapAfter);
ResourceRegistry resourceRegistry = resourceRegistry(session);
try {
double result = stream.mapToDouble(entity -> entity.id * 0.1D).max().getAsDouble();
assertTrue(resourceRegistry.hasRegisteredResources());
assertEquals(1, result, 0.1);
} finally {
stream.close();
assertFalse(resourceRegistry.hasRegisteredResources());
}
onCloseAssertion.run();
});
// Test call close explicitly
scope.inTransaction(session -> {
try (Stream<Long> stream = getLongStream(prepare, session, onCloseCallbacks, flatMapBefore, flatMapAfter)) {
ResourceRegistry resourceRegistry = resourceRegistry(session);
try {
Object[] result = stream.sorted().skip(5).limit(5).toArray();
assertTrue(resourceRegistry.hasRegisteredResources());
assertEquals(5, result.length);
assertEquals(6, result[0]);
assertEquals(10, result[4]);
} finally {
stream.close();
assertFalse(resourceRegistry.hasRegisteredResources());
}
onCloseAssertion.run();
}
});
// Test Java 9 Stream methods
scope.inTransaction(session -> {
Method takeWhileMethod = ReflectHelper.getMethod(Stream.class, "takeWhile", Predicate.class);
if (takeWhileMethod != null) {
try (Stream<Long> stream = getLongStream(prepare, session, onCloseCallbacks, flatMapBefore, flatMapAfter)) {
ResourceRegistry resourceRegistry = resourceRegistry(session);
try {
Predicate<Integer> predicate = id -> id <= 5;
Stream<Integer> takeWhileStream = (Stream<Integer>) takeWhileMethod.invoke(stream, predicate);
List<Integer> result = takeWhileStream.collect(Collectors.toList());
assertTrue(resourceRegistry.hasRegisteredResources());
assertEquals(5, result.size());
assertTrue(result.contains(1));
assertTrue(result.contains(3));
assertTrue(result.contains(5));
} finally {
stream.close();
assertFalse(resourceRegistry.hasRegisteredResources());
}
onCloseAssertion.run();
} catch (IllegalAccessException | InvocationTargetException e) {
fail("Could not execute takeWhile because of " + e.getMessage());
}
}
});
scope.inTransaction(session -> {
Method dropWhileMethod = ReflectHelper.getMethod(Stream.class, "dropWhile", Predicate.class);
if (dropWhileMethod != null) {
try (Stream<Long> stream = getLongStream(prepare, session, onCloseCallbacks, flatMapBefore, flatMapAfter)) {
ResourceRegistry resourceRegistry = resourceRegistry(session);
Predicate<Integer> predicate = id -> id <= 5;
Stream<Integer> dropWhileStream = (Stream<Integer>) dropWhileMethod.invoke(stream, predicate);
try {
List<Integer> result = dropWhileStream.collect(Collectors.toList());
assertTrue(resourceRegistry.hasRegisteredResources());
assertEquals(5, result.size());
assertTrue(result.contains(6));
assertTrue(result.contains(8));
assertTrue(result.contains(10));
} finally {
stream.close();
assertFalse(resourceRegistry.hasRegisteredResources());
}
onCloseAssertion.run();
} catch (IllegalAccessException | InvocationTargetException e) {
fail("Could not execute takeWhile because of " + e.getMessage());
}
}
});
}
use of org.hibernate.resource.jdbc.ResourceRegistry in project hibernate-orm by hibernate.
the class AbstractBatchImpl method releaseStatements.
protected void releaseStatements() {
final LinkedHashMap<String, PreparedStatement> statements = getStatements();
final ResourceRegistry resourceRegistry = jdbcCoordinator.getLogicalConnection().getResourceRegistry();
for (PreparedStatement statement : statements.values()) {
clearBatch(statement);
resourceRegistry.release(statement);
}
// IMPL NOTE: If the statements are not cleared and JTA is being used, then
// jdbcCoordinator.afterStatementExecution() will abort the batch and a
// warning will be logged. To avoid the warning, clear statements first,
// before calling jdbcCoordinator.afterStatementExecution().
statements.clear();
jdbcCoordinator.afterStatementExecution();
}
use of org.hibernate.resource.jdbc.ResourceRegistry in project hibernate-orm by hibernate.
the class AggressiveReleaseTest method testReleaseCircumventedManually.
@Test
public void testReleaseCircumventedManually() {
connectionProvider.clear();
connectionProvider.clear();
ResourceRegistry registry = sessionFactoryScope().fromSession(session -> {
JdbcCoordinatorImpl jdbcCoord = (JdbcCoordinatorImpl) session.getJdbcCoordinator();
ResourceRegistry resourceRegistry = jdbcCoord.getLogicalConnection().getResourceRegistry();
try {
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);
assertTrue(resourceRegistry.hasRegisteredResources());
assertEquals(1, connectionProvider.getAcquiredConnections().size());
assertEquals(0, connectionProvider.getReleasedConnections().size());
resourceRegistry.release(ps);
jdbcCoord.afterStatementExecution();
assertFalse(resourceRegistry.hasRegisteredResources());
assertEquals(0, connectionProvider.getAcquiredConnections().size());
assertEquals(1, connectionProvider.getReleasedConnections().size());
// disable releases...
jdbcCoord.disableReleases();
// open a result set...
ps = jdbcCoord.getStatementPreparer().prepareStatement("select * from SANDBOX_JDBC_TST");
jdbcCoord.getResultSetReturn().extract(ps);
assertTrue(resourceRegistry.hasRegisteredResources());
assertEquals(1, connectionProvider.getAcquiredConnections().size());
assertEquals(1, connectionProvider.getReleasedConnections().size());
// and close it...
resourceRegistry.release(ps);
jdbcCoord.afterStatementExecution();
// the release should be circumvented...
assertFalse(resourceRegistry.hasRegisteredResources());
assertEquals(1, connectionProvider.getAcquiredConnections().size());
assertEquals(1, connectionProvider.getReleasedConnections().size());
// let the close of the logical connection below release all resources (hopefully)...
} catch (SQLException sqle) {
fail("incorrect exception type : sqlexception");
} finally {
jdbcCoord.close();
}
return resourceRegistry;
});
assertFalse(registry.hasRegisteredResources());
assertEquals(0, connectionProvider.getAcquiredConnections().size());
assertEquals(2, connectionProvider.getReleasedConnections().size());
}
use of org.hibernate.resource.jdbc.ResourceRegistry in project hibernate-orm by hibernate.
the class AggressiveReleaseTest method testBasicRelease.
@Test
public void testBasicRelease() {
connectionProvider.clear();
ResourceRegistry registry = sessionFactoryScope().fromSession(session -> {
JdbcCoordinatorImpl jdbcCoord = (JdbcCoordinatorImpl) session.getJdbcCoordinator();
ResourceRegistry resourceRegistry = jdbcCoord.getLogicalConnection().getResourceRegistry();
try {
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);
assertTrue(jdbcCoord.getLogicalConnection().getResourceRegistry().hasRegisteredResources());
assertEquals(1, connectionProvider.getAcquiredConnections().size());
assertEquals(0, connectionProvider.getReleasedConnections().size());
resourceRegistry.release(ps);
jdbcCoord.afterStatementExecution();
assertFalse(resourceRegistry.hasRegisteredResources());
assertEquals(0, connectionProvider.getAcquiredConnections().size());
assertEquals(1, connectionProvider.getReleasedConnections().size());
} catch (SQLException sqle) {
fail("incorrect exception type : sqlexception");
} finally {
jdbcCoord.close();
}
return resourceRegistry;
});
assertFalse(registry.hasRegisteredResources());
}
use of org.hibernate.resource.jdbc.ResourceRegistry in project hibernate-orm by hibernate.
the class AggressiveReleaseTest method testReleaseCircumventedByHeldResources.
@Test
public void testReleaseCircumventedByHeldResources() {
connectionProvider.clear();
ResourceRegistry registry = sessionFactoryScope().fromSession(session -> {
JdbcCoordinatorImpl jdbcCoord = (JdbcCoordinatorImpl) session.getJdbcCoordinator();
ResourceRegistry resourceRegistry = jdbcCoord.getLogicalConnection().getResourceRegistry();
try {
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);
assertTrue(resourceRegistry.hasRegisteredResources());
assertEquals(1, connectionProvider.getAcquiredConnections().size());
assertEquals(0, connectionProvider.getReleasedConnections().size());
resourceRegistry.release(ps);
jdbcCoord.afterStatementExecution();
assertFalse(resourceRegistry.hasRegisteredResources());
assertEquals(0, connectionProvider.getAcquiredConnections().size());
assertEquals(1, connectionProvider.getReleasedConnections().size());
// open a result set and hold it open...
ps = jdbcCoord.getStatementPreparer().prepareStatement("select * from SANDBOX_JDBC_TST");
jdbcCoord.getResultSetReturn().extract(ps);
assertTrue(resourceRegistry.hasRegisteredResources());
assertEquals(1, connectionProvider.getAcquiredConnections().size());
assertEquals(1, connectionProvider.getReleasedConnections().size());
// open a second result set
PreparedStatement ps2 = jdbcCoord.getStatementPreparer().prepareStatement("select * from SANDBOX_JDBC_TST");
jdbcCoord.getResultSetReturn().execute(ps);
assertTrue(resourceRegistry.hasRegisteredResources());
assertEquals(1, connectionProvider.getAcquiredConnections().size());
assertEquals(1, connectionProvider.getReleasedConnections().size());
// and close it...
resourceRegistry.release(ps2);
jdbcCoord.afterStatementExecution();
// the release should be circumvented...
assertTrue(resourceRegistry.hasRegisteredResources());
assertEquals(1, connectionProvider.getAcquiredConnections().size());
assertEquals(1, connectionProvider.getReleasedConnections().size());
// let the close of the logical connection below release all resources (hopefully)...
} catch (SQLException sqle) {
fail("incorrect exception type : sqlexception");
} finally {
jdbcCoord.close();
}
return resourceRegistry;
});
assertFalse(registry.hasRegisteredResources());
assertEquals(0, connectionProvider.getAcquiredConnections().size());
assertEquals(2, connectionProvider.getReleasedConnections().size());
}
Aggregations