bug fixed for multi-savepoint

This commit is contained in:
wenshao
2012-11-24 22:17:29 +08:00
parent e70079eeb1
commit 78e31a1c16
12 changed files with 329 additions and 19 deletions
@@ -1,6 +1,7 @@
package com.alibaba.druid.bvt.pool;
import java.sql.Connection;
import java.sql.Savepoint;
import junit.framework.Assert;
import junit.framework.TestCase;
@@ -26,7 +27,9 @@ public class DruidPooledConnectionTest extends TestCase {
public void test_rollback() throws Exception {
Connection conn = dataSource.getConnection();
conn.rollback();
conn.rollback(null);
Savepoint savepoint = conn.setSavepoint("xx");
conn.rollback(savepoint);
conn.close();
}
@@ -0,0 +1,72 @@
package com.alibaba.druid.bvt.pool;
import java.sql.Connection;
import java.sql.Savepoint;
import java.sql.Statement;
import junit.framework.Assert;
import junit.framework.TestCase;
import com.alibaba.druid.mock.MockConnection;
import com.alibaba.druid.pool.DruidDataSource;
public class SavepointTest extends TestCase {
private DruidDataSource dataSource;
protected void setUp() throws Exception {
dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mock:xxx");
dataSource.setTestOnBorrow(false);
dataSource.setInitialSize(1);
}
protected void tearDown() throws Exception {
dataSource.close();
}
public void test_multi_savepoint() throws Exception {
Connection conn = dataSource.getConnection();
MockConnection physicalConn = conn.unwrap(MockConnection.class);
Assert.assertEquals(true, conn.getAutoCommit());
Assert.assertEquals(true, physicalConn.getAutoCommit());
conn.setAutoCommit(false);
Assert.assertEquals(false, conn.getAutoCommit());
Assert.assertEquals(false, physicalConn.getAutoCommit());
Savepoint[] savepoints = new Savepoint[100];
for (int i = 0; i < savepoints.length; ++i) {
Statement stmt = conn.createStatement();
stmt.execute("insert t (" + i + ")");
stmt.close();
savepoints[i] = conn.setSavepoint();
Assert.assertEquals(i + 1, physicalConn.getSavepoints().size());
for (int j = 0; j <= i; ++j) {
Assert.assertTrue(physicalConn.getSavepoints().contains(savepoints[j]));
}
}
// rollback single
conn.rollback(savepoints[99]);
Assert.assertEquals(99, physicalConn.getSavepoints().size());
// release single
conn.releaseSavepoint(savepoints[97]);
Assert.assertEquals(98, physicalConn.getSavepoints().size());
// rollback multi
conn.rollback(savepoints[90]);
Assert.assertEquals(90, physicalConn.getSavepoints().size());
// rollback all
conn.rollback();
Assert.assertEquals(0, physicalConn.getSavepoints().size());
conn.close();
}
}
@@ -17,6 +17,7 @@ package com.alibaba.druid.bvt.pool.basic;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import junit.framework.Assert;
@@ -90,8 +91,24 @@ public class ConnectionTest3 extends TestCase {
conn.setSavepoint();
conn.setSavepoint("savepoint");
conn.rollback();
conn.rollback(null);
conn.releaseSavepoint(null);
{
Exception error = null;
try {
conn.rollback(null);
} catch (SQLException e) {
error = e;
}
Assert.assertNotNull(error);
}
{
Exception error = null;
try {
conn.releaseSavepoint(null);
} catch (SQLException e) {
error = e;
}
Assert.assertNotNull(error);
}
conn.createBlob();
conn.createClob();
conn.createNClob();
@@ -191,7 +191,7 @@ public class BasicTypeTest extends TestCase {
rs.updateRow();
JdbcUtils.close(rs);
conn.rollback(point);
// conn.rollback(point);
conn.setAutoCommit(true);
rs = stmt.executeQuery("SELECT F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17 FROM T_BASIC_TYPE");
@@ -212,7 +212,7 @@ public class BasicTypeTest extends TestCase {
rs.getTimestamp("F11");
rs.getTimestamp("F11", Calendar.getInstance());
rs.getBytes("F12");
rs.getBlob("F12").free();
rs.getBlob("F12");
rs.getString("F13");
rs.getAsciiStream("F13");
rs.getCharacterStream("F14");
@@ -0,0 +1,75 @@
package com.alibaba.druid.test;
import javax.sql.DataSource;
import junit.framework.Assert;
import junit.framework.TestCase;
import org.nutz.dao.Chain;
import org.nutz.dao.Dao;
import org.nutz.dao.impl.NutDao;
import org.nutz.trans.Atom;
import org.nutz.trans.Trans;
import com.alibaba.druid.pool.DruidDataSource;
public class NutzTransactionTest extends TestCase {
private DataSource dataSource;
protected void setUp() throws Exception {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:jtds:sqlserver://192.168.1.105/petstore");
dataSource.setUsername("sa");
dataSource.setPassword("hello");
dataSource.setFilters("log4j");
// BasicDataSource dataSource = new BasicDataSource();
// dataSource.setDriverClassName("net.sourceforge.jtds.jdbc.Driver");
// dataSource.setUrl("jdbc:jtds:sqlserver://192.168.1.105/petstore");
// dataSource.setUsername("sa");
// dataSource.setPassword("hello");
this.dataSource = dataSource;
}
public void test_trans() throws Exception {
Dao dao = new NutDao(dataSource);
dao.clear("test");
// doTran1(dao);
doTran2(dao);
}
void doTran1(final Dao dao) {
try {
Trans.exec(new Atom() {
@Override
public void run() {
dao.insert("[test]", Chain.make("name", "1"));
throw new RuntimeException();
}
});
} catch (Exception e) {
}
Assert.assertEquals(0, dao.count("[test]"));
}
void doTran2(final Dao dao) {
try {
Trans.exec(new Atom() {
@Override
public void run() {
dao.insert("[test]", Chain.make("name", "1"));
dao.insert("[test]", Chain.make("name", "111111111111111111111111111111"));
}
});
} catch (Exception e) {
// e.printStackTrace();
}
Assert.assertEquals(0, dao.count("[test]"));
}
}