Skip to content

Commit a43e215

Browse files
authored
Merge pull request #5 from FOCONIS/master
Fix for preparedStatementCache on different schemas
2 parents 7dd7fd7 + 1f14df8 commit a43e215

4 files changed

Lines changed: 51 additions & 6 deletions

File tree

pom.xml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
<parent>
66
<groupId>org.avaje</groupId>
7-
<artifactId>oss-parent</artifactId>
7+
<artifactId>java8-parent</artifactId>
88
<version>1.1</version>
99
</parent>
1010

1111
<artifactId>avaje-datasource</artifactId>
12-
<version>1.1.5-SNAPSHOT</version>
12+
<version>2.1.1-SNAPSHOT</version>
1313

1414
<scm>
1515
<connection>scm:git:https://github.com/ebean-orm/avaje-datasource.git</connection>
@@ -19,7 +19,7 @@
1919
</scm>
2020

2121
<properties>
22-
<java.version>1.6</java.version>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2323
</properties>
2424

2525
<dependencies>
@@ -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/main/java/org/avaje/datasource/delegate/ConnectionDelegator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
public class ConnectionDelegator implements Connection {
99

1010
private final Connection delegate;
11+
12+
protected String currentSchema;
1113

1214
public ConnectionDelegator(Connection delegate) {
1315
this.delegate = delegate;
@@ -23,6 +25,7 @@ public Connection getDelegate() {
2325
@Override
2426
public void setSchema(String schema) throws SQLException {
2527
delegate.setSchema(schema);
28+
currentSchema = schema;
2629
}
2730

2831
@Override

src/main/java/org/avaje/datasource/pool/PooledConnection.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,15 +363,21 @@ void returnPreparedStatement(ExtendedPreparedStatement pstmt) {
363363
* This will try to use a cache of PreparedStatements.
364364
*/
365365
public PreparedStatement prepareStatement(String sql, int returnKeysFlag) throws SQLException {
366-
String cacheKey = sql + returnKeysFlag;
367-
return prepareStatement(sql, true, returnKeysFlag, cacheKey);
366+
StringBuilder cacheKey = new StringBuilder(sql.length() + 50);
367+
cacheKey.append(sql);
368+
cacheKey.append(':').append(currentSchema);
369+
cacheKey.append(':').append(returnKeysFlag);
370+
return prepareStatement(sql, true, returnKeysFlag, cacheKey.toString());
368371
}
369372

370373
/**
371374
* This will try to use a cache of PreparedStatements.
372375
*/
373376
public PreparedStatement prepareStatement(String sql) throws SQLException {
374-
return prepareStatement(sql, false, 0, sql);
377+
StringBuilder cacheKey = new StringBuilder(sql.length() + 50);
378+
cacheKey.append(sql);
379+
cacheKey.append(':').append(currentSchema);
380+
return prepareStatement(sql, false, 0, cacheKey.toString());
375381
}
376382

377383
/**

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)