mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Fix for DataSourcePool trim idle connections not firing frequently
enough
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
package com.avaje.ebeaninternal.server.lib.sql;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.config.DataSourceConfig;
|
||||
import com.avaje.ebeaninternal.server.core.DefaultBackgroundExecutor;
|
||||
import com.avaje.ebeaninternal.server.lib.sql.DataSourcePool.Status;
|
||||
@@ -15,38 +17,42 @@ public class TestDataSourceMax extends BaseTestCase {
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
boolean runThisManuallyNow = true;
|
||||
boolean skipThisTest = true;
|
||||
|
||||
if (!runThisManuallyNow) {
|
||||
if (skipThisTest) {
|
||||
return;
|
||||
}
|
||||
|
||||
Ebean.getServer(null);
|
||||
|
||||
String name = "h2";
|
||||
|
||||
DataSourceConfig dsConfig = new DataSourceConfig();
|
||||
dsConfig.loadSettings(name);
|
||||
dsConfig.setMinConnections(3);
|
||||
dsConfig.setMaxConnections(3);
|
||||
dsConfig.setMinConnections(2);
|
||||
dsConfig.setMaxConnections(8);
|
||||
dsConfig.setWaitTimeoutMillis(30000);
|
||||
dsConfig.setCaptureStackTrace(true);
|
||||
|
||||
DataSourcePool pool = new DataSourcePool(null, name, dsConfig);
|
||||
|
||||
Assert.assertEquals(3, pool.getMaxSize());
|
||||
// Assert.assertEquals(3, pool.getMaxSize());
|
||||
|
||||
DefaultBackgroundExecutor bg = new DefaultBackgroundExecutor(10, 2, 180, 30, "testDs");
|
||||
|
||||
try {
|
||||
for (int i = 0; i < 12; i++) {
|
||||
// Thread.sleep(10*i);
|
||||
bg.execute(new ConnRunner(pool, 100));
|
||||
bg.execute(new ConnRunner(pool, 4000));
|
||||
}
|
||||
|
||||
System.out.println("main thread sleep ... " + pool.getStatus(false));
|
||||
|
||||
Thread.sleep(1000);
|
||||
Thread.sleep(10000);
|
||||
Status status = pool.getStatus(false);
|
||||
System.out.println(status);
|
||||
|
||||
pool.shutdown(false);
|
||||
// this dumpOrder was for 3 vectors used in PooledConnectionQueue
|
||||
// that logged the order of wait, notify and obtain events
|
||||
// I have remove that code.
|
||||
@@ -71,14 +77,43 @@ public class TestDataSourceMax extends BaseTestCase {
|
||||
}
|
||||
|
||||
public void run() {
|
||||
Connection connection = null;
|
||||
PreparedStatement pstmt = null;
|
||||
ResultSet rset = null;
|
||||
try {
|
||||
Connection connection = pool.getConnection();
|
||||
connection = pool.getConnection();
|
||||
pstmt = connection.prepareStatement("select count(*) from o_customer");
|
||||
rset = pstmt.executeQuery();
|
||||
|
||||
System.out.println("sleep " + sleepMillis);
|
||||
Thread.sleep(sleepMillis);
|
||||
connection.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("sleep done");
|
||||
|
||||
} 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
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 test() {
|
||||
|
||||
FreeConnectionBuffer b = new FreeConnectionBuffer(3);
|
||||
Assert.assertEquals(0, b.size());
|
||||
|
||||
PooledConnection p0 = Mockito.mock(PooledConnection.class);
|
||||
PooledConnection p1 = Mockito.mock(PooledConnection.class);
|
||||
PooledConnection p2 = Mockito.mock(PooledConnection.class);
|
||||
b.add(p0);
|
||||
b.add(p1);
|
||||
b.add(p2);
|
||||
|
||||
Assert.assertEquals(3, b.size());
|
||||
|
||||
// add 1 second as this is going to fast for System.currentTimeMillis()
|
||||
long now = System.currentTimeMillis()+1000;
|
||||
int trimCount = b.trim(now);
|
||||
|
||||
Assert.assertEquals(0, b.size());
|
||||
Assert.assertEquals(3, trimCount);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithTime() {
|
||||
|
||||
FreeConnectionBuffer b = new FreeConnectionBuffer(3);
|
||||
Assert.assertEquals(0, b.size());
|
||||
|
||||
PooledConnection p0 = Mockito.mock(PooledConnection.class);
|
||||
Mockito.when(p0.getLastUsedTime()).thenReturn(1000l);
|
||||
|
||||
PooledConnection p1 = Mockito.mock(PooledConnection.class);
|
||||
Mockito.when(p1.getLastUsedTime()).thenReturn(2000l);
|
||||
|
||||
PooledConnection p2 = Mockito.mock(PooledConnection.class);
|
||||
Mockito.when(p2.getLastUsedTime()).thenReturn(1100l);
|
||||
|
||||
b.add(p0);
|
||||
b.add(p1);
|
||||
b.add(p2);
|
||||
|
||||
Assert.assertEquals(3, b.size());
|
||||
|
||||
int trimCount = b.trim(1500);
|
||||
|
||||
Assert.assertEquals(1, b.size());
|
||||
Assert.assertEquals(2, trimCount);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user