mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#618 - Refactor - remove DataSourcePool implementation as separate dependency
This commit is contained in:
@@ -1,100 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.lib.sql;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
|
||||
public class TestBusyBuffer extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
BusyConnectionBuffer b = new BusyConnectionBuffer(2, 4);
|
||||
|
||||
PooledConnection p0 = new PooledConnection("0");
|
||||
PooledConnection p1 = new PooledConnection("1");
|
||||
PooledConnection p2 = new PooledConnection("2");
|
||||
PooledConnection p3 = new PooledConnection("3");
|
||||
|
||||
Assert.assertEquals(2, b.getCapacity());
|
||||
b.add(p0);
|
||||
b.add(p1);
|
||||
Assert.assertEquals(2, b.getCapacity());
|
||||
b.add(p2);
|
||||
Assert.assertEquals(6, b.getCapacity());
|
||||
b.add(p3);
|
||||
|
||||
Assert.assertEquals(0, p0.getSlotId());
|
||||
Assert.assertEquals(1, p1.getSlotId());
|
||||
Assert.assertEquals(2, p2.getSlotId());
|
||||
Assert.assertEquals(3, p3.getSlotId());
|
||||
|
||||
b.remove(p2);
|
||||
b.add(p2);
|
||||
Assert.assertEquals(4, p2.getSlotId());
|
||||
|
||||
b.remove(p0);
|
||||
b.add(p0);
|
||||
Assert.assertEquals(5, p0.getSlotId());
|
||||
|
||||
b.remove(p2);
|
||||
b.add(p2);
|
||||
Assert.assertEquals(0, p2.getSlotId());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_rotate() {
|
||||
|
||||
BusyConnectionBuffer b = new BusyConnectionBuffer(2, 2);
|
||||
|
||||
PooledConnection p0 = new PooledConnection("0");
|
||||
PooledConnection p1 = new PooledConnection("1");
|
||||
PooledConnection p2 = new PooledConnection("2");
|
||||
PooledConnection p3 = new PooledConnection("3");
|
||||
|
||||
Assert.assertEquals(2, b.getCapacity());
|
||||
Assert.assertEquals(0, b.size());
|
||||
|
||||
b.add(p0);
|
||||
b.add(p1);
|
||||
Assert.assertEquals(2, b.size());
|
||||
Assert.assertEquals(2, b.getCapacity());
|
||||
b.add(p2);
|
||||
Assert.assertEquals(3, b.size());
|
||||
Assert.assertEquals(4, b.getCapacity());
|
||||
b.add(p3);
|
||||
Assert.assertEquals(4, b.size());
|
||||
Assert.assertEquals(4, b.getCapacity());
|
||||
|
||||
Assert.assertEquals(0, p0.getSlotId());
|
||||
Assert.assertEquals(1, p1.getSlotId());
|
||||
Assert.assertEquals(2, p2.getSlotId());
|
||||
Assert.assertEquals(3, p3.getSlotId());
|
||||
|
||||
b.remove(p2);
|
||||
Assert.assertEquals(3, b.size());
|
||||
b.remove(p0);
|
||||
Assert.assertEquals(2, b.size());
|
||||
b.remove(p3);
|
||||
Assert.assertEquals(1, b.size());
|
||||
b.add(p2);
|
||||
Assert.assertEquals(2, b.size());
|
||||
Assert.assertEquals(0, p2.getSlotId());
|
||||
|
||||
b.remove(p0);
|
||||
Assert.assertEquals(2, b.size());
|
||||
b.add(p0);
|
||||
Assert.assertEquals(3, b.size());
|
||||
|
||||
// p1 is still in it's slot
|
||||
Assert.assertEquals(2, p0.getSlotId());
|
||||
|
||||
b.remove(p2);
|
||||
b.add(p2);
|
||||
Assert.assertEquals(3, p2.getSlotId());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,131 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.lib.sql;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.config.DataSourceConfig;
|
||||
import com.avaje.ebeaninternal.server.core.DefaultBackgroundExecutor;
|
||||
import com.avaje.ebeaninternal.server.lib.sql.DataSourcePool.Status;
|
||||
|
||||
public class TestDataSourceMax extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
boolean skipThisTest = true;
|
||||
|
||||
if (skipThisTest) {
|
||||
return;
|
||||
}
|
||||
|
||||
String name = "mysql";
|
||||
|
||||
DataSourceConfig dsConfig = new DataSourceConfig();
|
||||
dsConfig.loadSettings(name);
|
||||
dsConfig.setMinConnections(2);
|
||||
dsConfig.setMaxConnections(25);
|
||||
dsConfig.setWaitTimeoutMillis(30000);
|
||||
dsConfig.setCaptureStackTrace(true);
|
||||
|
||||
DataSourcePool pool = new DataSourcePool(null, name, dsConfig);
|
||||
|
||||
|
||||
//pool.checkDataSource();
|
||||
|
||||
// if (true) {
|
||||
// pool.shutdown(false);
|
||||
// return;
|
||||
// }
|
||||
|
||||
DefaultBackgroundExecutor bg = new DefaultBackgroundExecutor(1, 1, 2, 180, 30, "testDs");
|
||||
|
||||
try {
|
||||
for (int i = 0; i < 12; i++) {
|
||||
// Thread.sleep(10*i);
|
||||
bg.execute(new ConnRunner(pool, 4000, i));
|
||||
}
|
||||
|
||||
Thread.sleep(10000);
|
||||
pool.getStatistics(true);
|
||||
|
||||
Thread.sleep(30000);
|
||||
|
||||
Status status = pool.getStatus(false);
|
||||
System.out.println(status);
|
||||
|
||||
pool.shutdown(false);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class ConnRunner implements Runnable {
|
||||
|
||||
final DataSourcePool pool;
|
||||
final long sleepMillis;
|
||||
final int position;
|
||||
|
||||
ConnRunner(DataSourcePool pool, long sleepMillis, int position) {
|
||||
this.pool = pool;
|
||||
this.sleepMillis = sleepMillis;
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
private void waitSomeTime(long count) {
|
||||
try {
|
||||
Thread.sleep(sleepMillis);
|
||||
} catch (InterruptedException e){
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void run() {
|
||||
Connection connection = null;
|
||||
PreparedStatement pstmt = null;
|
||||
ResultSet rset = null;
|
||||
long count = -1;
|
||||
try {
|
||||
connection = pool.getConnection();
|
||||
pstmt = connection.prepareStatement("select count(*) from o_customer");
|
||||
rset = pstmt.executeQuery();
|
||||
|
||||
while (rset.next()) {
|
||||
// do nothing actually
|
||||
count = rset.getLong(1);
|
||||
}
|
||||
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
if (rset != null) {
|
||||
try {
|
||||
rset.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (pstmt != null) {
|
||||
try {
|
||||
pstmt.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (connection != null) {
|
||||
try {
|
||||
connection.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
waitSomeTime(count);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.lib.sql;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
|
||||
public class TestFreeBuffer extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
FreeConnectionBuffer b = new FreeConnectionBuffer();
|
||||
|
||||
PooledConnection p0 = new PooledConnection("0");
|
||||
PooledConnection p1 = new PooledConnection("1");
|
||||
PooledConnection p2 = new PooledConnection("2");
|
||||
// PooledConnection p3 = new PooledConnection("3");
|
||||
|
||||
Assert.assertEquals(0, b.size());
|
||||
Assert.assertEquals(true, b.isEmpty());
|
||||
|
||||
b.add(p0);
|
||||
|
||||
Assert.assertEquals(1, b.size());
|
||||
Assert.assertEquals(false, b.isEmpty());
|
||||
|
||||
PooledConnection r0 = b.remove();
|
||||
Assert.assertTrue(p0 == r0);
|
||||
|
||||
Assert.assertEquals(0, b.size());
|
||||
Assert.assertEquals(true, b.isEmpty());
|
||||
|
||||
b.add(p0);
|
||||
b.add(p1);
|
||||
b.add(p2);
|
||||
|
||||
Assert.assertEquals(3, b.size());
|
||||
|
||||
PooledConnection r1 = b.remove();
|
||||
Assert.assertTrue(p0 == r1);
|
||||
PooledConnection r2 = b.remove();
|
||||
Assert.assertTrue(p1 == r2);
|
||||
|
||||
Assert.assertEquals(1, b.size());
|
||||
b.add(p0);
|
||||
Assert.assertEquals(2, b.size());
|
||||
PooledConnection r3 = b.remove();
|
||||
Assert.assertTrue(p2 == r3);
|
||||
Assert.assertEquals(1, b.size());
|
||||
PooledConnection r4 = b.remove();
|
||||
Assert.assertTrue(p0 == r4);
|
||||
Assert.assertEquals(0, b.size());
|
||||
|
||||
b.add(p2);
|
||||
b.add(p1);
|
||||
b.add(p0);
|
||||
|
||||
Assert.assertEquals(3, b.size());
|
||||
|
||||
PooledConnection r5 = b.remove();
|
||||
Assert.assertTrue(p2 == r5);
|
||||
Assert.assertEquals(2, b.size());
|
||||
|
||||
PooledConnection r6 = b.remove();
|
||||
Assert.assertTrue(p1 == r6);
|
||||
Assert.assertEquals(1, b.size());
|
||||
|
||||
PooledConnection r7 = b.remove();
|
||||
Assert.assertTrue(p0 == r7);
|
||||
Assert.assertEquals(0, b.size());
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.lib.sql;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
|
||||
public class TestFreeBufferTrim extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testWithTime() {
|
||||
|
||||
FreeConnectionBuffer b = new FreeConnectionBuffer();
|
||||
Assert.assertEquals(0, b.size());
|
||||
|
||||
PooledConnection p0 = Mockito.mock(PooledConnection.class);
|
||||
Mockito.when(p0.shouldTrim(1500, 0)).thenReturn(true);
|
||||
|
||||
PooledConnection p1 = Mockito.mock(PooledConnection.class);
|
||||
Mockito.when(p1.shouldTrim(1500, 0)).thenReturn(true);
|
||||
|
||||
PooledConnection p2 = Mockito.mock(PooledConnection.class);
|
||||
Mockito.when(p2.shouldTrim(1500, 0)).thenReturn(false);
|
||||
|
||||
b.add(p0);
|
||||
b.add(p1);
|
||||
b.add(p2);
|
||||
|
||||
Assert.assertEquals(3, b.size());
|
||||
|
||||
int trimCount = b.trim(1500, 0);
|
||||
|
||||
Assert.assertEquals(1, b.size());
|
||||
Assert.assertEquals(2, trimCount);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import com.avaje.ebean.EbeanServer;
|
||||
import com.avaje.ebean.EbeanServerFactory;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.ebean.SqlRow;
|
||||
import com.avaje.ebean.config.DataSourceConfig;
|
||||
import org.avaje.datasource.DataSourceConfig;
|
||||
import com.avaje.ebean.config.ServerConfig;
|
||||
import com.avaje.ebean.config.dbplatform.PostgresPlatform;
|
||||
import com.avaje.tests.model.basic.TOne;
|
||||
|
||||
@@ -2,7 +2,7 @@ package com.avaje.tests.basic;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
import com.avaje.ebeaninternal.server.lib.sql.DataSourcePoolListener;
|
||||
import org.avaje.datasource.DataSourcePoolListener;
|
||||
|
||||
public class MyTestDataSourcePoolListener implements DataSourcePoolListener
|
||||
{
|
||||
|
||||
@@ -5,16 +5,19 @@ import com.avaje.ebean.EbeanServer;
|
||||
import com.avaje.ebean.EbeanServerFactory;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.ebean.Transaction;
|
||||
import com.avaje.ebean.config.DataSourceConfig;
|
||||
import com.avaje.ebean.config.PropertyMap;
|
||||
import com.avaje.ebean.config.ServerConfig;
|
||||
import com.avaje.ebeaninternal.server.lib.sql.DataSourcePool;
|
||||
import com.avaje.tests.model.basic.UTDetail;
|
||||
import com.avaje.tests.model.basic.UTMaster;
|
||||
import org.avaje.datasource.DataSourceConfig;
|
||||
import org.avaje.datasource.DataSourcePool;
|
||||
import org.avaje.datasource.pool.ConnectionPool;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
@@ -24,11 +27,13 @@ public class TestAutoCommitDataSource extends BaseTestCase {
|
||||
@Test
|
||||
public void test() throws SQLException {
|
||||
|
||||
Properties properties = PropertyMap.defaultProperties();
|
||||
|
||||
DataSourceConfig dsConfig = new DataSourceConfig();
|
||||
dsConfig.loadSettings("h2autocommit");//"pg"
|
||||
dsConfig.loadSettings(properties, "h2autocommit");//"pg"
|
||||
dsConfig.setAutoCommit(true);
|
||||
|
||||
DataSourcePool pool = new DataSourcePool(null, "h2autocommit", dsConfig);
|
||||
DataSourcePool pool = new ConnectionPool("h2autocommit", dsConfig);
|
||||
|
||||
Connection connection = pool.getConnection();
|
||||
assertTrue(connection.getAutoCommit());
|
||||
|
||||
@@ -5,16 +5,19 @@ import com.avaje.ebean.EbeanServer;
|
||||
import com.avaje.ebean.EbeanServerFactory;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.ebean.Transaction;
|
||||
import com.avaje.ebean.config.DataSourceConfig;
|
||||
import com.avaje.ebean.config.PropertyMap;
|
||||
import com.avaje.ebean.config.ServerConfig;
|
||||
import com.avaje.ebeaninternal.server.lib.sql.DataSourcePool;
|
||||
import com.avaje.tests.model.basic.UTDetail;
|
||||
import com.avaje.tests.model.basic.UTMaster;
|
||||
import org.avaje.datasource.DataSourceConfig;
|
||||
import org.avaje.datasource.DataSourcePool;
|
||||
import org.avaje.datasource.pool.ConnectionPool;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
@@ -24,11 +27,13 @@ public class TestExplicitTransactionMode extends BaseTestCase {
|
||||
@Test
|
||||
public void test() throws SQLException {
|
||||
|
||||
Properties properties = PropertyMap.defaultProperties();
|
||||
|
||||
DataSourceConfig dsConfig = new DataSourceConfig();
|
||||
dsConfig.loadSettings("h2autocommit");//"h2autocommit","pg"
|
||||
dsConfig.loadSettings(properties, "h2autocommit");//"h2autocommit","pg"
|
||||
dsConfig.setAutoCommit(true);
|
||||
|
||||
DataSourcePool pool = new DataSourcePool(null, "h2autocommit", dsConfig);
|
||||
DataSourcePool pool = new ConnectionPool("h2autocommit", dsConfig);
|
||||
|
||||
Connection connection = pool.getConnection();
|
||||
assertTrue(connection.getAutoCommit());
|
||||
|
||||
@@ -8,7 +8,7 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.avaje.ebean.EbeanServer;
|
||||
import com.avaje.ebean.EbeanServerFactory;
|
||||
import com.avaje.ebean.config.DataSourceConfig;
|
||||
import org.avaje.datasource.DataSourceConfig;
|
||||
import com.avaje.ebean.config.ServerConfig;
|
||||
import com.avaje.tests.model.basic.TOne;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user