-
Notifications
You must be signed in to change notification settings - Fork 213
/
C3P0PoolTest.java
52 lines (42 loc) · 1.86 KB
/
C3P0PoolTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
Copyright 2009-2019 Igor Polevoy
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.
*/
package org.javalite.activejdbc;
import org.javalite.activejdbc.test_models.Person;
import com.mchange.v2.c3p0.DataSources;
import org.junit.Test;
import javax.sql.DataSource;
import java.beans.PropertyVetoException;
import java.sql.SQLException;
import static org.javalite.common.Convert.toSqlDate;
import static org.javalite.test.jspec.JSpec.a;
import static org.javalite.activejdbc.test.JdbcProperties.*;
/**
* Example of using c3p0 Pool
*
* @author Igor Polevoy
*/
public class C3P0PoolTest {
@Test
public void shouldUseConnectionFromPool() throws PropertyVetoException, SQLException, ClassNotFoundException {
Class.forName(driver());
DataSource dataSourceUnpooled = DataSources.unpooledDataSource(url(), user(), password());
DataSource dataSourcePooled = DataSources.pooledDataSource(dataSourceUnpooled); //init the connection pool
Base.open(dataSourcePooled); //get connection from pool
Person.deleteAll(); //clean DB before test
Person.createIt("name", "Matt", "last_name", "Diamont", "dob", toSqlDate("1962-01-01"));
a(Person.findAll().size()).shouldBeEqual(1);
Person.deleteAll();//clean DB after test
Base.close();// really connection goes back to pool
DataSources.destroy(dataSourcePooled);//shut down the pool
}
}