Add Hibernate 7 batches 2-6: mapping styles, JPA annotations, natural IDs, @Immutable, stored procedures, in-memory test databases, JNDI mocking, proxies, associations, temporal mapping, named queries, HQL, Criteria API and EntityManager bootstrapping (Hibernate 7.4.5.Final + Spring Boot 4.1.1 + JDK 25)
Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01EQNA6DJ9VgCtW6zhCE8Xud
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
package com.ankurm.hibernatedemo.testdb;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Backs docs/09-testing-in-memory-databases.md, chapter "What DB_CLOSE_DELAY=-1 actually does". Raw JDBC, no
|
||||
* Hibernate, to isolate exactly the H2-specific behaviour rather than anything the ORM layer
|
||||
* might paper over.
|
||||
*/
|
||||
class DbCloseDelayTest {
|
||||
|
||||
private static final Logger DEMO = LoggerFactory.getLogger("DEMO");
|
||||
|
||||
@Test
|
||||
void withoutCloseDelay_dataDisappearsWhenTheLastConnectionCloses() throws Exception {
|
||||
String url = "jdbc:h2:mem:no-close-delay-demo"; // no DB_CLOSE_DELAY at all
|
||||
try (Connection c1 = DriverManager.getConnection(url, "sa", "")) {
|
||||
try (Statement st = c1.createStatement()) {
|
||||
st.execute("create table t (id integer)");
|
||||
st.execute("insert into t values (1)");
|
||||
}
|
||||
} // c1 closes here -- this is H2's LAST open connection to this in-memory DB
|
||||
|
||||
// A brand new connection to the SAME url now gets a BRAND NEW, EMPTY database --
|
||||
// querying the table we just created throws, because it no longer exists.
|
||||
try (Connection c2 = DriverManager.getConnection(url, "sa", "");
|
||||
Statement st = c2.createStatement()) {
|
||||
SQLException ex = assertThrows(SQLException.class, () -> st.execute("select * from t"));
|
||||
DEMO.info("without DB_CLOSE_DELAY=-1, reconnecting after the last close throws: {}: {}",
|
||||
ex.getClass().getSimpleName(), ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void withCloseDelayMinusOne_dataSurvivesAcrossConnectionCloses() throws Exception {
|
||||
String url = "jdbc:h2:mem:with-close-delay-demo;DB_CLOSE_DELAY=-1";
|
||||
try (Connection c1 = DriverManager.getConnection(url, "sa", "")) {
|
||||
try (Statement st = c1.createStatement()) {
|
||||
st.execute("create table t (id integer)");
|
||||
st.execute("insert into t values (1)");
|
||||
}
|
||||
} // c1 closes -- but DB_CLOSE_DELAY=-1 tells H2 to keep the in-memory DB alive anyway
|
||||
|
||||
try (Connection c2 = DriverManager.getConnection(url, "sa", "");
|
||||
Statement st = c2.createStatement()) {
|
||||
var rs = st.executeQuery("select id from t");
|
||||
assertThat(rs.next()).isTrue();
|
||||
assertThat(rs.getInt(1)).isEqualTo(1);
|
||||
DEMO.info("with DB_CLOSE_DELAY=-1, reconnecting after the last close still sees the row inserted earlier");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user