BAEL-1818 - A Guide to Connection Pools in Java (#4735)
* Strange git issue with README.MD, wouldn't revert the file * Initial Commit * Initial Commit * Update pom.xml * Update pom.xml * Initial Commit * Update pom.xml * Update source files * Update source files * Update source files * Update source files * Update Application.java * Update pom.xml * Update HikariCPDataSourceUnitTest class * Update HikariCPDataSourceUnitTest.java * Update pom.xml * Update unit test classes * Update BasicConnectionPoolUnitTest.java * Fix indentation in DBCDDataSource class * Update DBCPDataSource.java * Update BasicConnectionPool class * Update BasicConnectionPool class * Update BasicConnectionPool.java * Update BasicConnectionPool.java * Update pom.xml * Update pom.xml * BAEL-1818 Refactored getConnection(), added shutdown(), cleaned up pom.xml * BAEL-1818 Removed getConnectionPool(), upgraded c3po version * BAEL-1818 Deleted obsolete connectionpool module * BAEL-1818 Deleted obsolete connectionpool module
This commit is contained in:
committed by
Predrag Maric
parent
86daa854d2
commit
ac801b4319
@@ -0,0 +1,69 @@
|
||||
package com.baeldung.connectionpool;
|
||||
|
||||
import com.baeldung.connectionpool.connectionpools.BasicConnectionPool;
|
||||
import com.baeldung.connectionpool.connectionpools.ConnectionPool;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class BasicConnectionPoolUnitTest {
|
||||
|
||||
private static ConnectionPool connectionPool;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpBasicConnectionPoolInstance() throws SQLException {
|
||||
connectionPool = BasicConnectionPool.create("jdbc:h2:mem:test", "user", "password");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBasicConnectionPoolInstance_whenCalledgetConnection_thenCorrect() throws Exception {
|
||||
assertTrue(connectionPool.getConnection().isValid(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBasicConnectionPoolInstance_whenCalledreleaseConnection_thenCorrect() throws Exception {
|
||||
Connection connection = connectionPool.getConnection();
|
||||
assertThat(connectionPool.releaseConnection(connection)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBasicConnectionPoolInstance_whenCalledgetUrl_thenCorrect() {
|
||||
assertThat(connectionPool.getUrl()).isEqualTo("jdbc:h2:mem:test");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBasicConnectionPoolInstance_whenCalledgetUser_thenCorrect() {
|
||||
assertThat(connectionPool.getUser()).isEqualTo("user");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBasicConnectionPoolInstance_whenCalledgetPassword_thenCorrect() {
|
||||
assertThat(connectionPool.getPassword()).isEqualTo("password");
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
public void givenBasicConnectionPoolInstance_whenAskedForMoreThanMax_thenError() throws Exception {
|
||||
// this test needs to be independent so it doesn't share the same connection pool as other tests
|
||||
ConnectionPool cp = BasicConnectionPool.create("jdbc:h2:mem:test", "user", "password");
|
||||
final int MAX_POOL_SIZE = 20;
|
||||
for (int i = 0; i < MAX_POOL_SIZE + 1; i++) {
|
||||
cp.getConnection();
|
||||
}
|
||||
fail();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBasicConnectionPoolInstance_whenSutdown_thenEmpty() throws Exception {
|
||||
ConnectionPool cp = BasicConnectionPool.create("jdbc:h2:mem:test", "user", "password");
|
||||
assertThat(((BasicConnectionPool)cp).getSize()).isEqualTo(10);
|
||||
|
||||
((BasicConnectionPool) cp).shutdown();
|
||||
assertThat(((BasicConnectionPool)cp).getSize()).isEqualTo(0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.connectionpool;
|
||||
|
||||
import com.baeldung.connectionpool.connectionpools.C3poDataSource;
|
||||
import java.sql.SQLException;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Test;
|
||||
|
||||
public class C3poDataSourceUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenC3poDataSourceClass_whenCalledgetConnection_thenCorrect() throws SQLException {
|
||||
assertTrue(C3poDataSource.getConnection().isValid(1));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.connectionpool;
|
||||
|
||||
import com.baeldung.connectionpool.connectionpools.DBCPDataSource;
|
||||
import java.sql.SQLException;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DBCPDataSourceUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenDBCPDataSourceClass_whenCalledgetConnection_thenCorrect() throws SQLException {
|
||||
assertTrue(DBCPDataSource.getConnection().isValid(1));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.connectionpool;
|
||||
|
||||
import com.baeldung.connectionpool.connectionpools.HikariCPDataSource;
|
||||
import java.sql.SQLException;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Test;
|
||||
|
||||
public class HikariCPDataSourceUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenHikariDataSourceClass_whenCalledgetConnection_thenCorrect() throws SQLException {
|
||||
assertTrue(HikariCPDataSource.getConnection().isValid(1));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user