X Tutup
Skip to content

Commit 93a611f

Browse files
committed
- Added new initConnectionPool() and terminateConnectionPool() methods to the Database class.
- Updated default "User-Agent" header in the HTTP Request class. git-svn-id: svn://192.168.0.80/JavaXT/javaxt-core@1095 2c7b0aa6-e0b2-3c4e-bb4a-8b65b6c465ff
1 parent e8f687f commit 93a611f

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

src/javaxt/http/Request.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ private void initHeaders(){
156156
this.setHeader("Accept-Encoding", "gzip,deflate");
157157
this.setHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
158158
//this.setHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.10)"); //windows xp
159-
this.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0"); //windows 7
159+
this.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0"); //windows 7
160160
//this.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0"); //windows 8
161161
}
162162

src/javaxt/sql/Database.java

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class Database implements Cloneable {
2727
private ConnectionPoolDataSource ConnectionPoolDataSource;
2828
private static final Class<?>[] stringType = { String.class };
2929
private static final Class<?>[] integerType = { Integer.TYPE };
30+
private ConnectionPool connectionPool;
3031

3132

3233
//**************************************************************************
@@ -410,16 +411,63 @@ else if (vendor.equals("Derby") || vendor.equals("SQLite")){
410411
//**************************************************************************
411412
//** getConnection
412413
//**************************************************************************
413-
/** Used to open a connection to the database. Note the the connection will
414-
* need to be closed afterwards.
414+
/** Used to open a connection to the database. If a connection pool has been
415+
* initialized (initConnectionPool), then an open connection is returned
416+
* from the pool. Otherwise, a new connection is created. In either case,
417+
* the connection must be closed when you are finished with it.
415418
*/
416419
public Connection getConnection() throws SQLException {
417-
Connection connection = new Connection();
418-
connection.open(this);
419-
return connection;
420+
if (connectionPool==null){
421+
Connection connection = new Connection();
422+
connection.open(this);
423+
return connection;
424+
}
425+
else{
426+
return connectionPool.getConnection();
427+
}
420428
}
421429

422430

431+
//**************************************************************************
432+
//** initConnectionPool
433+
//**************************************************************************
434+
/** Used to initialize a connection pool. Subsequent called to the
435+
* getConnection() method will return connections from the pool.
436+
*/
437+
public void initConnectionPool(int maxConnections) throws SQLException {
438+
if (connectionPool!=null) return;
439+
connectionPool = new ConnectionPool(this, maxConnections);
440+
441+
//Create Shutdown Hook to clean up the connection pool on exit
442+
Runtime.getRuntime().addShutdownHook(new Thread() {
443+
public void run() {
444+
if (connectionPool!=null){
445+
System.out.println("Shutting down...");
446+
try{
447+
connectionPool.close();
448+
}
449+
catch(Exception e){
450+
e.printStackTrace();
451+
}
452+
}
453+
}
454+
});
455+
}
456+
457+
458+
//**************************************************************************
459+
//** terminateConnectionPool
460+
//**************************************************************************
461+
/** Used to terminate the connection pool, closing all active connections.
462+
*/
463+
public void terminateConnectionPool() throws SQLException {
464+
if (connectionPool!=null){
465+
connectionPool.close();
466+
connectionPool = null;
467+
}
468+
}
469+
470+
423471
//**************************************************************************
424472
//** setConnectionPoolDataSource
425473
//**************************************************************************

0 commit comments

Comments
 (0)
X Tutup