Skip to content

Commit

Permalink
Replace normal JDBC connection with c3p0 pool connection
Browse files Browse the repository at this point in the history
  • Loading branch information
richard committed Jul 7, 2020
1 parent 6396143 commit d7b901a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 16 deletions.
Binary file added lib/c3p0-0.9.5.5.jar
Binary file not shown.
Binary file added lib/mchange-commons-java-0.2.19.jar
Binary file not shown.
6 changes: 5 additions & 1 deletion nbproject/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,21 @@ dist.jlink.dir=${dist.dir}/jlink
dist.jlink.output=${dist.jlink.dir}/b2b-jcl
endorsed.classpath=
excludes=
file.reference.c3p0-0.9.5.5.jar=lib/c3p0-0.9.5.5.jar
file.reference.hamcrest-core-1.3.jar=lib/hamcrest-core-1.3.jar
file.reference.junit-4.12.jar=lib/junit-4.12.jar
file.reference.mariadb-java-client-2.6.1.jar=lib/mariadb-java-client-2.6.1.jar
file.reference.mchange-commons-java-0.2.19.jar=lib/mchange-commons-java-0.2.19.jar
file.reference.slf4j-api-1.7.30.jar=lib/slf4j-api-1.7.30.jar
file.reference.slf4j-simple-1.7.30.jar=lib/slf4j-simple-1.7.30.jar
includes=**
jar.compress=false
javac.classpath=\
${file.reference.slf4j-api-1.7.30.jar}:\
${file.reference.slf4j-simple-1.7.30.jar}:\
${file.reference.mariadb-java-client-2.6.1.jar}
${file.reference.mariadb-java-client-2.6.1.jar}:\
${file.reference.c3p0-0.9.5.5.jar}:\
${file.reference.mchange-commons-java-0.2.19.jar}
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
Expand Down
46 changes: 32 additions & 14 deletions src/cz/b2b/jcl/DB/JdbcClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 &#60;[email protected]&#620;
Expand All @@ -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
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);

}

Expand All @@ -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);

}

Expand All @@ -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);

}

Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion test/cz/b2b/jcl/DB/JdbcClassLoaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void testJDBC() throws Exception {
JdbcClassLoader childClassLoader = new JdbcClassLoader(Thread.currentThread().getContextClassLoader());
childClassLoader.setConnection(driver, dbUrl, table, username, password);

final Class<?> test = Class.forName("cz.b2b.jcl.RAM.resource.Test", true, childClassLoader);
Class<?> test = Class.forName("cz.b2b.jcl.RAM.resource.Test", true, childClassLoader);
Object o = test.getDeclaredConstructor(new Class[]{}).newInstance(new Object[]{});

Method print = o.getClass().getMethod("print", String.class);
Expand Down

0 comments on commit d7b901a

Please sign in to comment.