Skip to content

Commit

Permalink
Added C3P0 connection pool to the SQL utils.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Jul 29, 2015
1 parent d244e09 commit 87b5e82
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 13 deletions.
@@ -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);
}
});
}

}
11 changes: 3 additions & 8 deletions rapidoid-quick/src/main/java/org/rapidoid/quick/Quick.java
Expand Up @@ -20,8 +20,6 @@
* #L%
*/

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
Expand Down Expand Up @@ -56,20 +54,17 @@ public static void serve(Application app, String[] args, Object... config) {
}

public static void bootstrap(Application app, final String[] args, Object... config) {
Apps.bootstrap(app, args, config);

Applications.main().setDefaultApp(app);
Applications.main().register(app);

Ctxs.open();
Ctxs.setPersisterProvider(new QuickJPA(config));

HibernateDBPlugin db = new HibernateDBPlugin();

Plugins.register(new HibernateDBPlugin());
Plugins.register(new AppClasspathEntitiesPlugin());

List<Object> appConfig = U.<Object> list(config);
appConfig.add(db);
Apps.bootstrap(app, args, U.array(appConfig));

// TODO provide better support for javax.transaction.Transactional
AOP.register(Transactional.class, new TransactionInterceptor());

Expand Down
15 changes: 10 additions & 5 deletions rapidoid-sql/pom.xml
Expand Up @@ -22,6 +22,16 @@
<artifactId>rapidoid-log</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.2.1</version>
</dependency>
<dependency>
<groupId>org.rapidoid</groupId>
<artifactId>rapidoid-test-appctx-helper</artifactId>
Expand All @@ -46,11 +56,6 @@
<version>1.4.187</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.3.2</version>
</dependency>
</dependencies>

</project>
@@ -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();
}

}
12 changes: 12 additions & 0 deletions rapidoid-sql/src/test/java/org/rapidoid/sql/SQLTest.java
Expand Up @@ -50,12 +50,24 @@ public void testWithH2() {
insertAndCheckData();
}

@Test
public void testWithH2AndC3P0() {
new C3P0ConnectionPool(SQL.h2().db("test"));
insertAndCheckData();
}

@Test
public void testWithHSQLDB() {
SQL.hsql().db("test");
insertAndCheckData();
}

@Test
public void testWithHSQLDBAndC3P0() {
new C3P0ConnectionPool(SQL.hsql().db("test"));
insertAndCheckData();
}

private void insertAndCheckData() {
SQL.tryToExecute("DROP TABLE movie");
SQL.execute("CREATE TABLE movie (id int, title varchar(99))");
Expand Down

0 comments on commit 87b5e82

Please sign in to comment.