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
@@ -17,6 +17,7 @@ package com.alibaba.druid.filter.logging;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.sql.Types;
import com.alibaba.druid.filter.FilterChain;
@@ -290,6 +291,30 @@ public abstract class LogFilter extends FilterEventAdapter implements LogFilterM
connectionLog("{conn-" + connection.getId() + "} connected");
}
}
@Override
public Savepoint connection_setSavepoint(FilterChain chain, ConnectionProxy connection) throws SQLException {
Savepoint savepoint = chain.connection_setSavepoint(connection);
if (isConnectionLogEnabled()) {
connectionLog("{conn " + connection.getId() + "} setSavepoint-" + savepoint.getSavepointId());
}
return savepoint;
}
@Override
public Savepoint connection_setSavepoint(FilterChain chain, ConnectionProxy connection, String name)
throws SQLException {
Savepoint savepoint = chain.connection_setSavepoint(connection, name);
if (isConnectionLogEnabled()) {
connectionLog("{conn " + connection.getId() + "} setSavepoint-" + name);
}
return savepoint;
}
@Override
public void connection_rollback(FilterChain chain, ConnectionProxy connection) throws SQLException {
@@ -299,6 +324,15 @@ public abstract class LogFilter extends FilterEventAdapter implements LogFilterM
connectionLog("{conn " + connection.getId() + "} rollback");
}
}
@Override
public void connection_rollback(FilterChain chain, ConnectionProxy connection, Savepoint savePoint) throws SQLException {
super.connection_rollback(chain, connection, savePoint);
if (connectionRollbackAfterLogEnable && isConnectionLogEnabled()) {
connectionLog("{conn " + connection.getId() + "} rollback -> " + savePoint.getSavepointId());
}
}
@Override
public void connection_commit(FilterChain chain, ConnectionProxy connection) throws SQLException {
@@ -30,6 +30,8 @@ import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;
@@ -40,14 +42,16 @@ public class MockConnection extends ConnectionBase implements Connection {
// private final static Log LOG = LogFactory.getLog(MockConnection.class);
private boolean closed = false;
private boolean closed = false;
private MockDriver driver;
private MockDriver driver;
private int savepointIdSeed = 0;
private List<Savepoint> savepoints = new ArrayList<Savepoint>();
private long id;
private long id;
private final long createdTimeMillis = System.currentTimeMillis();
private long lastActiveTimeMillis = System.currentTimeMillis();
private final long createdTimeMillis = System.currentTimeMillis();
private long lastActiveTimeMillis = System.currentTimeMillis();
public MockConnection(){
this(null, null, null);
@@ -63,6 +67,10 @@ public class MockConnection extends ConnectionBase implements Connection {
}
}
public List<Savepoint> getSavepoints() {
return savepoints;
}
public long getLastActiveTimeMillis() {
return lastActiveTimeMillis;
}
@@ -168,6 +176,7 @@ public class MockConnection extends ConnectionBase implements Connection {
throw new MockConnectionClosedException();
}
this.savepoints.clear();
}
@Override
@@ -248,7 +257,12 @@ public class MockConnection extends ConnectionBase implements Connection {
if (closed) {
throw new MockConnectionClosedException();
}
return null;
MockSavepoint savepoint = new MockSavepoint();
savepoint.setSavepointId(this.savepointIdSeed++);
this.savepoints.add(savepoint);
return savepoint;
}
@Override
@@ -256,7 +270,13 @@ public class MockConnection extends ConnectionBase implements Connection {
if (closed) {
throw new MockConnectionClosedException();
}
return null;
MockSavepoint savepoint = new MockSavepoint();
savepoint.setSavepointId(this.savepointIdSeed++);
savepoint.setSavepointName(name);
this.savepoints.add(savepoint);
return savepoint;
}
@Override
@@ -264,6 +284,15 @@ public class MockConnection extends ConnectionBase implements Connection {
if (closed) {
throw new MockConnectionClosedException();
}
int index = this.savepoints.indexOf(savepoint);
if (index == -1) {
throw new SQLException("savepoint not contained");
}
for (int i = savepoints.size() - 1; i >= index; --i) {
savepoints.remove(i);
}
}
@Override
@@ -271,6 +300,16 @@ public class MockConnection extends ConnectionBase implements Connection {
if (closed) {
throw new MockConnectionClosedException();
}
if (savepoint == null) {
throw new SQLException("argument is null");
}
int index = this.savepoints.indexOf(savepoint);
if (index == -1) {
throw new SQLException("savepoint not contained");
}
savepoints.remove(savepoint);
}
@Override
@@ -0,0 +1,41 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.mock;
import java.sql.Savepoint;
public class MockSavepoint implements Savepoint {
private int savepointId;
private String savepointName;
public int getSavepointId() {
return savepointId;
}
public void setSavepointId(int savepointId) {
this.savepointId = savepointId;
}
public String getSavepointName() {
return savepointName;
}
public void setSavepointName(String savepointName) {
this.savepointName = savepointName;
}
}
@@ -736,7 +736,7 @@ public class DruidPooledConnection implements javax.sql.PooledConnection, Connec
} catch (SQLException ex) {
handleException(ex);
} finally {
handleEndTransaction(dataSource);
handleEndTransaction(dataSource, null);
}
}
@@ -762,7 +762,7 @@ public class DruidPooledConnection implements javax.sql.PooledConnection, Connec
} catch (SQLException ex) {
handleException(ex);
} finally {
handleEndTransaction(dataSource);
handleEndTransaction(dataSource, null);
}
}
@@ -787,12 +787,12 @@ public class DruidPooledConnection implements javax.sql.PooledConnection, Connec
} catch (SQLException ex) {
handleException(ex);
} finally {
handleEndTransaction(dataSource);
handleEndTransaction(dataSource, savepoint);
}
}
private void handleEndTransaction(DruidAbstractDataSource dataSource) {
if (transactionInfo != null) {
private void handleEndTransaction(DruidAbstractDataSource dataSource, Savepoint savepoint) {
if (transactionInfo != null && savepoint == null) {
transactionInfo.setEndTimeMillis();
long transactionMillis = transactionInfo.getEndTimeMillis() - transactionInfo.getStartTimeMillis();
@@ -1,3 +1,18 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.sqlserver.ast.stmt;
import java.util.ArrayList;
@@ -21,7 +21,7 @@ import java.util.List;
public class TransactionInfo {
private final long id;
private final List<String> sqlList = new ArrayList<String>();
private final List<String> sqlList = new ArrayList<String>(4);
private final long startTimeMillis;
private long endTimeMillis;