Skip to content

Commit 6cea829

Browse files
committed
Proof of concept of TrustedContext implementation
1 parent 031c614 commit 6cea829

File tree

4 files changed

+773
-0
lines changed

4 files changed

+773
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.ebean.datasource.tcdriver;
2+
3+
import io.ebean.datasource.DataSourceConnection;
4+
import io.ebean.datasource.DataSourcePool;
5+
import io.ebean.datasource.DataSourcePoolListener;
6+
7+
import java.sql.SQLException;
8+
9+
/**
10+
* Listener, that sets up TrustedConnection properly
11+
*
12+
* @author Roland Praml, Foconis Analytics GmbH
13+
*/
14+
public class TrustedContextListener implements DataSourcePoolListener {
15+
private ThreadLocal<String> user = new ThreadLocal<>();
16+
private ThreadLocal<String> pass = new ThreadLocal<>();
17+
private ThreadLocal<String> schema = new ThreadLocal<>();
18+
19+
@Override
20+
public void onAfterBorrowConnection(DataSourcePool pool, DataSourceConnection connection) {
21+
try {
22+
TrustedDb2Connection trustedDb2Connection = connection.unwrap(TrustedDb2Connection.class);
23+
if (trustedDb2Connection.switchUser(user.get(), pass.get())) {
24+
connection.clearPreparedStatementCache();
25+
}
26+
connection.setSchema(schema.get());
27+
//System.out.println("Switched to " + user.get() + ", Schema: " + schema.get());
28+
} catch (SQLException e) {
29+
throw new RuntimeException(e); // TODO: Allow throwing sqlException here
30+
}
31+
}
32+
33+
public void setContext(String user, String pass, String schema) {
34+
this.user.set(user);
35+
this.pass.set(pass);
36+
this.schema.set(schema);
37+
}
38+
}

0 commit comments

Comments
 (0)