22
33import oracle .jdbc .pool .OracleDataSource ;
44
5+ import javax .sql .DataSource ;
6+ import java .io .PrintWriter ;
57import java .sql .CallableStatement ;
68import java .sql .Connection ;
9+ import java .sql .ConnectionBuilder ;
710import java .sql .SQLException ;
11+ import java .sql .SQLFeatureNotSupportedException ;
12+ import java .util .logging .Logger ;
813
9- public class InitializableOracleDataSource extends OracleDataSource {
14+ public class InitializableOracleDataSource implements DataSource {
1015
16+ private final OracleDataSource delegate ;
1117 private String initSql ;
1218
1319 public InitializableOracleDataSource () throws SQLException {
20+ this .delegate = new OracleDataSource ();
1421 }
1522
23+ // --- URL / credentials passthrough (mirrors what callers used before) ---
24+
25+ public void setURL (String url ) throws SQLException {
26+ delegate .setURL (url );
27+ }
28+
29+ public void setUser (String user ) {
30+ delegate .setUser (user );
31+ }
32+
33+ public void setPassword (String password ) {
34+ delegate .setPassword (password );
35+ }
36+
37+ // --- Core DataSource methods ---
38+
1639 @ Override
1740 public Connection getConnection () throws SQLException {
18- Connection con = super .getConnection ();
19-
20- if ( initSql != null && !initSql .isEmpty () ) {
21- try (CallableStatement stmt = con .prepareCall (initSql )) {
22- stmt .execute ();
23- }
24- }
41+ Connection con = delegate .getConnection ();
42+ runInitSql (con );
43+ return con ;
44+ }
2545
46+ @ Override
47+ public Connection getConnection (String username , String password ) throws SQLException {
48+ Connection con = delegate .getConnection (username , password );
49+ runInitSql (con );
2650 return con ;
2751 }
2852
29- public void setConnectionInitSql ( String sql ) {
53+ public void setConnectionInitSql (String sql ) {
3054 this .initSql = sql ;
3155 }
32- }
56+
57+ // --- DataSource boilerplate ---
58+
59+ @ Override
60+ public PrintWriter getLogWriter () throws SQLException {
61+ return delegate .getLogWriter ();
62+ }
63+
64+ @ Override
65+ public void setLogWriter (PrintWriter out ) throws SQLException {
66+ delegate .setLogWriter (out );
67+ }
68+
69+ @ Override
70+ public void setLoginTimeout (int seconds ) throws SQLException {
71+ delegate .setLoginTimeout (seconds );
72+ }
73+
74+ @ Override
75+ public int getLoginTimeout () throws SQLException {
76+ return delegate .getLoginTimeout ();
77+ }
78+
79+ @ Override
80+ public Logger getParentLogger () throws SQLFeatureNotSupportedException {
81+ return delegate .getParentLogger ();
82+ }
83+
84+ @ Override
85+ public <T > T unwrap (Class <T > iface ) throws SQLException {
86+ if (iface .isInstance (delegate )) return iface .cast (delegate );
87+ return delegate .unwrap (iface );
88+ }
89+
90+ @ Override
91+ public boolean isWrapperFor (Class <?> iface ) throws SQLException {
92+ return iface .isInstance (delegate ) || delegate .isWrapperFor (iface );
93+ }
94+
95+ // --- Private helpers ---
96+
97+ private void runInitSql (Connection con ) throws SQLException {
98+ if (initSql != null && !initSql .isEmpty ()) {
99+ try (CallableStatement stmt = con .prepareCall (initSql )) {
100+ stmt .execute ();
101+ }
102+ }
103+ }
104+ }
0 commit comments