Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added C3P0 connection pool to the SQL utils.
- Loading branch information
1 parent
d244e09
commit 87b5e82
Showing
5 changed files
with
190 additions
and
13 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
rapidoid-integration-tests/src/test/java/org/rapidoid/test/JDBCPoolC3P0Test.java
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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package org.rapidoid.test; | ||
|
||
/* | ||
* #%L | ||
* rapidoid-integration-tests | ||
* %% | ||
* Copyright (C) 2014 - 2015 Nikolche Mihajlovski and contributors | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
|
||
import java.util.Map; | ||
|
||
import org.junit.Test; | ||
import org.rapidoid.annotation.Authors; | ||
import org.rapidoid.annotation.Since; | ||
import org.rapidoid.config.Conf; | ||
import org.rapidoid.sql.C3P0ConnectionPool; | ||
import org.rapidoid.sql.SQL; | ||
import org.rapidoid.util.U; | ||
import org.rapidoid.util.UTILS; | ||
|
||
@Authors("Nikolche Mihajlovski") | ||
@Since("4.1.0") | ||
public class JDBCPoolC3P0Test extends IntegrationTestCommons { | ||
|
||
@Test(timeout = 30000) | ||
public void testJDBCPoolC3P0() { | ||
|
||
new C3P0ConnectionPool(SQL.h2()); | ||
|
||
SQL.execute("create table abc (id int, name varchar)"); | ||
SQL.execute("insert into abc values (?, ?)", 123, "xyz"); | ||
|
||
final Map<String, ?> expected = U.map("id", 123, "name", "xyz"); | ||
|
||
UTILS.benchmarkMT(100, "select", 100000, new Runnable() { | ||
@Override | ||
public void run() { | ||
Map<String, Object> record = U.single(SQL.get("select id, name from abc")); | ||
record = UTILS.lowercase(record); | ||
eq(record, expected); | ||
} | ||
}); | ||
} | ||
|
||
@Test(timeout = 30000) | ||
public void testJDBCWithTextconfig() { | ||
|
||
Conf.args("jdbc.url=jdbc:h2:mem:mydb", "jdbc.username=sa"); | ||
|
||
new C3P0ConnectionPool(SQL.defaultInstance()); | ||
|
||
SQL.execute("create table abc (id int, name varchar)"); | ||
SQL.execute("insert into abc values (?, ?)", 123, "xyz"); | ||
|
||
final Map<String, ?> expected = U.map("id", 123, "name", "xyz"); | ||
|
||
UTILS.benchmarkMT(100, "select", 100000, new Runnable() { | ||
@Override | ||
public void run() { | ||
Map<String, Object> record = U.single(SQL.get("select id, name from abc")); | ||
record = UTILS.lowercase(record); | ||
eq(record, expected); | ||
} | ||
}); | ||
} | ||
|
||
} |
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
85 changes: 85 additions & 0 deletions
85
rapidoid-sql/src/main/java/org/rapidoid/sql/C3P0ConnectionPool.java
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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package org.rapidoid.sql; | ||
|
||
/* | ||
* #%L | ||
* rapidoid-sql | ||
* %% | ||
* Copyright (C) 2014 - 2015 Nikolche Mihajlovski and contributors | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
|
||
import java.beans.PropertyVetoException; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
|
||
import org.rapidoid.annotation.Authors; | ||
import org.rapidoid.annotation.Since; | ||
import org.rapidoid.util.U; | ||
|
||
import com.mchange.v2.c3p0.ComboPooledDataSource; | ||
|
||
@Authors("Nikolche Mihajlovski") | ||
@Since("4.1.0") | ||
public class C3P0ConnectionPool implements ConnectionPool { | ||
|
||
private final ComboPooledDataSource pool = new ComboPooledDataSource(); | ||
|
||
public C3P0ConnectionPool(String jdbcUrl, String driverClass, String username, String password) { | ||
init(jdbcUrl, driverClass, username, password); | ||
} | ||
|
||
public C3P0ConnectionPool(String jdbcUrl, String driverClass) { | ||
this(jdbcUrl, driverClass, null, null); | ||
} | ||
|
||
public C3P0ConnectionPool(SQLAPI sqlapi) { | ||
this(sqlapi.url(), sqlapi.driver(), sqlapi.user(), sqlapi.password()); | ||
sqlapi.connectionPool(this); | ||
} | ||
|
||
private void init(String jdbcUrl, String driverClass, String username, String password) { | ||
try { | ||
pool.setDriverClass(driverClass); | ||
} catch (PropertyVetoException e) { | ||
throw U.rte("Cannot load JDBC driver!", e); | ||
} | ||
|
||
pool.setJdbcUrl(jdbcUrl); | ||
pool.setUser(username); | ||
pool.setPassword(password); | ||
|
||
pool.setMinPoolSize(5); | ||
pool.setAcquireIncrement(5); | ||
pool.setMaxPoolSize(20); | ||
} | ||
|
||
@Override | ||
public Connection getConnection(String jdbcUrl) throws SQLException { | ||
U.must(U.eq(jdbcUrl, pool.getJdbcUrl()), "The JDBC URLs don't match: '%s' and '%s'!", jdbcUrl, | ||
pool.getJdbcUrl()); | ||
return pool.getConnection(); | ||
} | ||
|
||
@Override | ||
public Connection getConnection(String jdbcUrl, String username, String password) throws SQLException { | ||
return pool.getConnection(username, password); | ||
} | ||
|
||
@Override | ||
public void releaseConnection(Connection connection) throws SQLException { | ||
connection.close(); | ||
} | ||
|
||
} |
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