Skip to content

Commit 0694b35

Browse files
committed
ADD: Testcase that demonstrates a caching issue with prepared statements
1 parent a580f96 commit 0694b35

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@
4444
<scope>test</scope>
4545
</dependency>
4646

47+
<!-- Override com.h2database version to get schema support -->
48+
<dependency>
49+
<groupId>com.h2database</groupId>
50+
<artifactId>h2</artifactId>
51+
<version>1.4.193</version>
52+
<scope>test</scope>
53+
</dependency>
54+
4755
</dependencies>
4856

4957
<build>

src/test/java/org/avaje/datasource/pool/ConnectionPoolTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import java.sql.Connection;
99
import java.sql.PreparedStatement;
1010
import java.sql.SQLException;
11+
import java.sql.Statement;
12+
13+
import javax.sql.DataSource;
1114

1215
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
1316

@@ -116,4 +119,29 @@ public void getDelegate() throws SQLException {
116119
assertThat(underlying).isInstanceOf(org.h2.jdbc.JdbcConnection.class);
117120
connection.close();
118121
}
122+
123+
@Test
124+
public void testSchemaSwitch() throws SQLException {
125+
Connection conn = pool.getConnection();
126+
Statement stmt = conn.createStatement();
127+
stmt.executeUpdate("CREATE SCHEMA TENANT_1");
128+
stmt.executeUpdate("CREATE SCHEMA TENANT_2");
129+
stmt.executeUpdate("CREATE TABLE TENANT_1.LOCAL_MODEL (id integer)");
130+
stmt.executeUpdate("CREATE TABLE TENANT_2.LOCAL_MODEL (id integer)");
131+
stmt.close();
132+
133+
conn.setSchema("TENANT_1");
134+
PreparedStatement ps1 = conn.prepareStatement("SELECT * from local_model");
135+
ps1.close();
136+
PreparedStatement ps2 = conn.prepareStatement("SELECT * from local_model");
137+
ps2.close();
138+
139+
conn.setSchema("TENANT_2");
140+
PreparedStatement ps3 = conn.prepareStatement("SELECT * from local_model");
141+
ps3.close();
142+
143+
144+
assertThat(ps1).isSameAs(ps2); // test if pstmtCache is working
145+
assertThat(ps1).isNotSameAs(ps3); // test if datasource recognize schema change
146+
}
119147
}

0 commit comments

Comments
 (0)