mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
bug fixed for discardConnection
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
package com.alibaba.druid.bvt.hibernate;
|
||||
|
||||
import com.alibaba.druid.bvt.hibernate.entity.Sample;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
import java.util.Properties;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.Transaction;
|
||||
@@ -10,9 +13,9 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
import java.util.Properties;
|
||||
import com.alibaba.druid.bvt.hibernate.entity.Sample;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
/**
|
||||
* @author yinheli <yinheli@gmail.com>
|
||||
@@ -59,6 +62,7 @@ public class HibernateCRUDTest extends TestCase {
|
||||
@Override
|
||||
public void tearDown() throws Exception {
|
||||
sessionFactory.close();
|
||||
JdbcUtils.close(dataSource);
|
||||
}
|
||||
|
||||
private void doCreate(Session session) {
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.mock.MockDriver;
|
||||
import com.alibaba.druid.mock.MockStatementBase;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.vendor.MySqlExceptionSorter;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
public class DiscardTest extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
private MockDriver driver;
|
||||
|
||||
private volatile boolean failed = false;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
driver = new MockDriver() {
|
||||
|
||||
public ResultSet executeQuery(MockStatementBase stmt, String sql) throws SQLException {
|
||||
if (failed) {
|
||||
throw new SQLException("", "", 1040);
|
||||
}
|
||||
return super.executeQuery(stmt, sql);
|
||||
}
|
||||
|
||||
public Connection connect(String url, Properties info) throws SQLException {
|
||||
while (failed) {
|
||||
try {
|
||||
Thread.sleep(1);
|
||||
} catch (InterruptedException e) {
|
||||
throw new SQLException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
return super.connect(url, info);
|
||||
}
|
||||
};
|
||||
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setTestOnBorrow(false);
|
||||
dataSource.setDriver(driver);
|
||||
dataSource.setDbType("mysql");
|
||||
dataSource.setMinIdle(1);
|
||||
dataSource.setMaxActive(1);
|
||||
dataSource.setFilters("log4j");
|
||||
dataSource.setExceptionSorter(new MySqlExceptionSorter());
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_db_fail() throws Exception {
|
||||
exec();
|
||||
|
||||
final int THREAD_COUNT = 10;
|
||||
final CountDownLatch endLatch = new CountDownLatch(THREAD_COUNT);
|
||||
|
||||
Thread[] threads = new Thread[THREAD_COUNT];
|
||||
|
||||
{
|
||||
Exception error = null;
|
||||
|
||||
Connection conn = dataSource.getConnection();
|
||||
Statement stmt = conn.createStatement();
|
||||
|
||||
for (int i = 0; i < THREAD_COUNT; ++i) {
|
||||
threads[i] = new Thread() {
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
exec();
|
||||
} finally {
|
||||
endLatch.countDown();
|
||||
}
|
||||
}
|
||||
};
|
||||
threads[i].start();
|
||||
}
|
||||
|
||||
this.failed = true;
|
||||
try {
|
||||
ResultSet rs = stmt.executeQuery("select 1");
|
||||
rs.close();
|
||||
} catch (SQLException e) {
|
||||
error = e;
|
||||
}
|
||||
|
||||
stmt.close();
|
||||
conn.close();
|
||||
|
||||
Assert.assertNotNull(error);
|
||||
}
|
||||
|
||||
this.failed = false;
|
||||
|
||||
endLatch.await();
|
||||
}
|
||||
|
||||
private void exec() {
|
||||
Connection conn = null;
|
||||
Statement stmt = null;
|
||||
ResultSet rs = null;
|
||||
|
||||
try {
|
||||
conn = dataSource.getConnection();
|
||||
stmt = conn.createStatement();
|
||||
rs = stmt.executeQuery("select 1");
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
JdbcUtils.close(rs);
|
||||
JdbcUtils.close(stmt);
|
||||
JdbcUtils.close(conn);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user