ASF Bugzilla – Attachment 31838 Details for
Bug 53367
Database failure may cause pool to hang
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
Test demonstrating jdbc-pool connection leakage
TomcatPoolTest.java (text/x-java), 4.84 KB, created by
Anton
on 2014-07-22 01:36:19 UTC
(
hide
)
Description:
Test demonstrating jdbc-pool connection leakage
Filename:
MIME Type:
Creator:
Anton
Created:
2014-07-22 01:36:19 UTC
Size:
4.84 KB
patch
obsolete
>package test; > >import junit.framework.Assert; >import org.apache.tomcat.jdbc.pool.ConnectionPool; >import org.apache.tomcat.jdbc.pool.DataSource; >import org.apache.tomcat.jdbc.pool.PoolExhaustedException; >import org.apache.tomcat.jdbc.pool.PoolProperties; >import org.junit.Test; > >import java.sql.Connection; >import java.sql.DriverManager; >import java.sql.SQLException; >import java.util.ArrayList; >import java.util.List; >import java.util.concurrent.CountDownLatch; >import java.util.concurrent.atomic.AtomicInteger; > >public class TomcatPoolTest { > > @Test > public void testPool() throws SQLException, InterruptedException { > DriverManager.setLoginTimeout(1); > PoolProperties poolProperties = new PoolProperties(); > poolProperties.setUrl("jdbc:mysql://localhost:3306?zeroDateTimeBehavior=round&useUnicode=true&characterEncoding=UTF-8"); > poolProperties.setDriverClassName("com.mysql.jdbc.Driver"); > int threadsCount = 3; > poolProperties.setMaxActive(threadsCount); > poolProperties.setMaxIdle(threadsCount); > poolProperties.setMinIdle(0); > poolProperties.setMaxWait(5000); > poolProperties.setInitialSize(0); > poolProperties.setRemoveAbandoned(true); > poolProperties.setRemoveAbandonedTimeout(30000); > poolProperties.setRollbackOnReturn(true); > poolProperties.setUsername("root"); > poolProperties.setPassword("root"); > final DataSource ds = new DataSource(poolProperties); > > final CountDownLatch openedLatch = new CountDownLatch(threadsCount); > final CountDownLatch closedLatch = new CountDownLatch(threadsCount); > final CountDownLatch toCloseLatch = new CountDownLatch(1); > > for (int i = 0; i < threadsCount; i++) { > new Thread(new Runnable() { > @Override > public void run() { > try { > Connection connection = ds.getConnection(); > openedLatch.countDown(); > > toCloseLatch.await(); > connection.close(); > > closedLatch.countDown(); > > } catch (Exception e) { > e.printStackTrace(); > } > } > }).start(); > } > > openedLatch.await(); > ConnectionPool pool = ds.getPool(); > //Now we have 3 initialized busy connections > Assert.assertEquals(0, pool.getIdle()); > Assert.assertEquals(3, pool.getActive()); > Assert.assertEquals(threadsCount, pool.getSize()); > > List<Thread> threads = new ArrayList<Thread>(); > for (int i = 0; i < threadsCount; i++) { > Thread thread = new Thread(new Runnable() { > @Override > public void run() { > try { > ds.getConnection(); > } catch (Exception e) { > e.printStackTrace(); > } > } > }); > thread.start(); > threads.add(thread); > } > for (Thread thread : threads) { > thread.interrupt(); > } > for (Thread thread : threads) { > thread.join(); > } > //Still 3 active connections > Assert.assertEquals(0, pool.getIdle()); > Assert.assertEquals(3, pool.getActive()); > Assert.assertEquals(threadsCount, pool.getSize()); > > toCloseLatch.countDown(); > closedLatch.await(); > > //Here comes the bug! No more active connections and unable to establish new connections. > > Assert.assertEquals(0, pool.getIdle()); // <-- Should be threadsCount (3) here > Assert.assertEquals(0, pool.getActive()); > Assert.assertEquals(threadsCount, pool.getSize()); > > final AtomicInteger failedCount = new AtomicInteger(); > threads.clear(); > for (int i = 0; i < threadsCount; i++) { > Thread thread = new Thread(new Runnable() { > @Override > public void run() { > try { > ds.getConnection(); > } catch (PoolExhaustedException e) { > failedCount.incrementAndGet(); > e.printStackTrace(); > } catch (Exception e) { > e.printStackTrace(); > throw new RuntimeException(e); > } > } > }); > thread.start(); > threads.add(thread); > } > > for (Thread thread : threads) { > thread.join(); > } > Assert.assertEquals(threadsCount, failedCount.get()); > > Assert.assertEquals(0, pool.getIdle()); > Assert.assertEquals(0, pool.getActive()); > Assert.assertEquals(threadsCount, pool.getSize()); > } >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 53367
:
31838
|
31840