-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace normal JDBC connection with c3p0 pool connection
- Loading branch information
richard
committed
Jul 7, 2020
1 parent
6396143
commit d7b901a
Showing
5 changed files
with
38 additions
and
16 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,8 @@ | |
import org.slf4j.*; | ||
import cz.b2b.jcl.util.CONST; | ||
import java.sql.*; | ||
import com.mchange.v2.c3p0.*; | ||
import java.beans.PropertyVetoException; | ||
|
||
/** | ||
The JdbcClassLoader class implements a class loader that loads classes from a | ||
|
@@ -66,7 +68,7 @@ Example of the structure of the relevant table (mysql, mariadb): | |
System.out.println("class = " + o.getClass().getCanonicalName()); | ||
print.invoke(o, "JDBC"); | ||
</pre> | ||
</pre> | ||
@author Richard Kotal <[email protected]ɬ | ||
|
@@ -79,13 +81,10 @@ public class JdbcClassLoader extends URLClassLoader { | |
private final static String class_name = "class_name"; | ||
private final static String class_code = "class_code"; | ||
|
||
private String driver = null; | ||
private String dbUrl = null; | ||
private String username = null; | ||
private String password = null; | ||
private String table = null; | ||
|
||
private final URL jdbcURL = new URL(protocol, CONST.host, CONST.port, CONST.baseURI, new JdbcURLStreamHandler()); | ||
private final ComboPooledDataSource cpds = new ComboPooledDataSource(); | ||
|
||
/** | ||
Constructs a new JdbcClassLoader for the given URLs of URLClassLoader and | ||
|
@@ -122,16 +121,35 @@ public JdbcClassLoader(ClassLoader parent) throws MalformedURLException { | |
this(new URL[]{}, parent); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
cpds.close(); | ||
super.close(); | ||
|
||
} | ||
|
||
/** | ||
Set Pool settings. | ||
@param minPoolSize minimal pool size | ||
@param maxPoolSize maximal pool size | ||
@param maxIdleTime maximal idle timeout | ||
*/ | ||
public void setPoolSettings(int minPoolSize, int maxPoolSize, int maxIdleTime) { | ||
cpds.setMinPoolSize(minPoolSize); | ||
cpds.setMaxPoolSize(maxPoolSize); | ||
cpds.setMaxIdleTime(maxIdleTime); | ||
|
||
} | ||
|
||
/** | ||
Set JDBC driver. | ||
@param driver JDBC driver (ex.: org.mariadb.jdbc.Driver) | ||
@throws ClassNotFoundException Thrown when JDBC driver class not found | ||
*/ | ||
public void setDriver(String driver) throws ClassNotFoundException { | ||
this.driver = driver; | ||
Class.forName(this.driver); | ||
|
||
public void setDriver(String driver) throws ClassNotFoundException, PropertyVetoException { | ||
cpds.setDriverClass(driver); | ||
} | ||
|
||
/** | ||
|
@@ -148,7 +166,7 @@ public void setTable(String table) { | |
@param dbUrl url connection (ex.: jdbc:mariadb://127.0.0.1:3306/test) | ||
*/ | ||
public void setDbUrl(String dbUrl) { | ||
this.dbUrl = dbUrl; | ||
cpds.setJdbcUrl(dbUrl); | ||
|
||
} | ||
|
||
|
@@ -157,7 +175,7 @@ public void setDbUrl(String dbUrl) { | |
@param username login name to DB (ex.: root) | ||
*/ | ||
public void setUsername(String username) { | ||
this.username = username; | ||
cpds.setUser(username); | ||
|
||
} | ||
|
||
|
@@ -166,7 +184,7 @@ public void setUsername(String username) { | |
@param password password to DB (ex.: root) | ||
*/ | ||
public void setPassword(String password) { | ||
this.password = password; | ||
cpds.setPassword(password); | ||
|
||
} | ||
|
||
|
@@ -179,7 +197,7 @@ public void setPassword(String password) { | |
@param password password to DB (ex.: root) | ||
@throws ClassNotFoundException Thrown when JDBC driver class not found | ||
*/ | ||
public void setConnection(String driver, String dbUrl, String table, String username, String password) throws ClassNotFoundException { | ||
public void setConnection(String driver, String dbUrl, String table, String username, String password) throws ClassNotFoundException, PropertyVetoException { | ||
setDriver(driver); | ||
setDbUrl(dbUrl); | ||
setTable(table); | ||
|
@@ -230,7 +248,7 @@ private byte[] class_code(Map cols) { | |
logger.debug(SQL); | ||
|
||
try { | ||
conn = DriverManager.getConnection(dbUrl, username, password); | ||
conn = cpds.getConnection(); | ||
stmt = conn.createStatement(); | ||
rs = stmt.executeQuery(SQL); | ||
if (rs.next() == true) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters