77import  java .io .Closeable ;
88import  java .io .PrintWriter ;
99import  java .sql .Connection ;
10+ import  java .sql .DriverManager ;
1011import  java .sql .SQLException ;
1112import  java .sql .SQLTimeoutException ;
1213import  java .util .List ;
1314import  java .util .logging .Logger ;
14- import  javax .sql .ConnectionPoolDataSource ;
15- import  javax .sql .DataSource ;
16- import  javax .sql .XAConnection ;
17- import  javax .sql .XADataSource ;
18- import  org .mariadb .jdbc .pool .MariaDbInnerPoolConnection ;
15+ import  javax .sql .*;
1916import  org .mariadb .jdbc .pool .Pool ;
2017import  org .mariadb .jdbc .pool .Pools ;
2118
2219public  class  MariaDbPoolDataSource 
2320 implements  DataSource , ConnectionPoolDataSource , XADataSource , Closeable , AutoCloseable  {
2421
25-  private  final  Pool  pool ;
22+  private  Pool  pool ;
23+  private  Configuration  conf  = null ;
24+  private  String  url  = null ;
25+  private  String  user  = null ;
26+  private  String  password  = null ;
27+  private  Integer  loginTimeout  = null ;
28+ 
29+  public  MariaDbPoolDataSource () {}
2630
2731 public  MariaDbPoolDataSource (String  url ) throws  SQLException  {
2832 if  (Configuration .acceptsUrl (url )) {
29-  Configuration  conf  = Configuration .parse (url );
33+  this .url  = url ;
34+  conf  = Configuration .parse (url );
3035 pool  = Pools .retrievePool (conf );
3136 } else  {
3237 throw  new  SQLException (String .format ("Wrong mariaDB url: %s" , url ));
3338 }
3439 }
3540
41+  private  void  config () throws  SQLException  {
42+  if  (url  == null ) throw  new  SQLException ("url not set" );
43+  conf  = Configuration .parse (url );
44+  if  (loginTimeout  != null ) conf .connectTimeout (loginTimeout  * 1000 );
45+  if  (user  != null ) {
46+  conf  = conf .clone (user , password );
47+  } else  {
48+  user  = conf .user ();
49+  password  = conf .password ();
50+  }
51+  pool  = Pools .retrievePool (conf );
52+  }
53+ 
3654 /** 
3755 * Attempts to establish a connection with the data source that this {@code DataSource} object 
3856 * represents. 
@@ -45,6 +63,7 @@ public MariaDbPoolDataSource(String url) throws SQLException {
4563 */ 
4664 @ Override 
4765 public  Connection  getConnection () throws  SQLException  {
66+  if  (conf  == null ) config ();
4867 return  pool .getPoolConnection ().getConnection ();
4968 }
5069
@@ -62,6 +81,7 @@ public Connection getConnection() throws SQLException {
6281 */ 
6382 @ Override 
6483 public  Connection  getConnection (String  username , String  password ) throws  SQLException  {
84+  if  (conf  == null ) config ();
6585 return  pool .getPoolConnection (username , password ).getConnection ();
6686 }
6787
@@ -139,7 +159,9 @@ public void setLogWriter(PrintWriter out) {}
139159 */ 
140160 @ Override 
141161 public  int  getLoginTimeout () {
142-  return  pool .getConf ().connectTimeout () / 1000 ;
162+  if  (loginTimeout  != null ) return  loginTimeout ;
163+  if  (conf  != null ) return  conf .connectTimeout () / 1000 ;
164+  return  DriverManager .getLoginTimeout () > 0  ? DriverManager .getLoginTimeout () : 30 ;
143165 }
144166
145167 /** 
@@ -149,12 +171,13 @@ public int getLoginTimeout() {
149171 * is created, the login timeout is initially 30s. 
150172 * 
151173 * @param seconds the data source login time limit 
174+  * @throws SQLException if wrong configuration set 
152175 * @see #getLoginTimeout 
153-  * @since 1.4 
154176 */ 
155177 @ Override 
156-  public  void  setLoginTimeout (int  seconds ) {
157-  pool .getConf ().connectTimeout (seconds  * 1000 );
178+  public  void  setLoginTimeout (int  seconds ) throws  SQLException  {
179+  loginTimeout  = seconds ;
180+  if  (conf  != null ) config ();
158181 }
159182
160183 /** 
@@ -168,37 +191,84 @@ public Logger getParentLogger() {
168191 }
169192
170193 @ Override 
171-  public  MariaDbInnerPoolConnection  getPooledConnection () throws  SQLException  {
194+  public  PooledConnection  getPooledConnection () throws  SQLException  {
195+  if  (conf  == null ) config ();
172196 return  pool .getPoolConnection ();
173197 }
174198
175199 @ Override 
176-  public  MariaDbInnerPoolConnection  getPooledConnection (String  username , String  password )
200+  public  PooledConnection  getPooledConnection (String  username , String  password )
177201 throws  SQLException  {
202+  if  (conf  == null ) config ();
178203 return  pool .getPoolConnection (username , password );
179204 }
180205
181206 @ Override 
182207 public  XAConnection  getXAConnection () throws  SQLException  {
208+  if  (conf  == null ) config ();
183209 return  pool .getPoolConnection ();
184210 }
185211
186212 @ Override 
187213 public  XAConnection  getXAConnection (String  username , String  password ) throws  SQLException  {
214+  if  (conf  == null ) config ();
188215 return  pool .getPoolConnection (username , password );
189216 }
190217
218+  /** 
219+  * Sets the URL for this datasource 
220+  * 
221+  * @param url connection string 
222+  * @throws SQLException if url is not accepted 
223+  */ 
224+  public  void  setUrl (String  url ) throws  SQLException  {
225+  if  (Configuration .acceptsUrl (url )) {
226+  this .url  = url ;
227+  config ();
228+  } else  {
229+  throw  new  SQLException (String .format ("Wrong mariaDB url: %s" , url ));
230+  }
231+  }
232+ 
233+  /** 
234+  * Returns the URL for this datasource 
235+  * 
236+  * @return the URL for this datasource 
237+  */ 
238+  public  String  getUrl () {
239+  if  (conf  == null ) return  url ;
240+  return  conf .initialUrl ();
241+  }
242+ 
243+  public  String  getUser () {
244+  return  user ;
245+  }
246+ 
247+  public  void  setUser (String  user ) throws  SQLException  {
248+  this .user  = user ;
249+  if  (conf  != null ) config ();
250+  }
251+ 
252+  public  String  getPassword () {
253+  return  password ;
254+  }
255+ 
256+  public  void  setPassword (String  password ) throws  SQLException  {
257+  this .password  = password ;
258+  if  (conf  != null ) config ();
259+  }
260+ 
191261 /** Close datasource. */ 
192262 public  void  close () {
193263 try  {
194-  pool .close ();
264+  if  ( pool  !=  null )  pool .close ();
195265 } catch  (Exception  interrupted ) {
196266 // eat 
197267 }
198268 }
199269
200270 public  String  getPoolName () {
201-  return  pool .getPoolTag ();
271+  return  ( pool  !=  null ) ?  pool .getPoolTag () :  null ;
202272 }
203273
204274 /** 
@@ -207,6 +277,6 @@ public String getPoolName() {
207277 * @return current thread id's 
208278 */ 
209279 public  List <Long > testGetConnectionIdleThreadIds () {
210-  return  pool .testGetConnectionIdleThreadIds ();
280+  return  ( pool  !=  null ) ?  pool .testGetConnectionIdleThreadIds () :  null ;
211281 }
212282}
0 commit comments