NutzCN Logo
问答 Trans.exec()后为什么select @@@@TranCount是2呢?造成Atom后还有个阻塞的事务存在。
发布于 1058天前 作者 天空 955 次浏览 复制 上一个帖子 下一个帖子
标签:

代码:

public User editGoodsItem()
	{
		getBaseDao().execute(sql9);
		int tranCount1 = sql9.getInt();
		System.out.println("1***************tranCount:" + tranCount1);
			
		// 事务绑定
		Trans.exec(Connection.TRANSACTION_REPEATABLE_READ, new Atom() {
			public void run() {
				getBaseDao().execute(sql9);
				int tranCount = sql9.getInt();
				System.out.println("2***************tranCount:" + tranCount);
			}
		});

	}

后台打印:
DEBUG: org.nutz.dao.impl.sql.run.NutDaoExecutor - select @@TranCount
1***************tranCount:0
DEBUG: org.nutz.dao.impl.sql.run.NutDaoExecutor - select @@TranCount
2***************tranCount:2

7 回复

设计个DataSource,打印conn被调用过的方法

上面代码没贴全

public User editGoodsItem()
	{
		final String ssql = "select @@@@TranCount ";
		final Sql sql9;
		sql9 = Sqls.create(ssql);
		sql9.setCallback(Sqls.callback.integer());

		getBaseDao().execute(sql9);
		int tranCount1 = sql9.getInt();
		System.out.println("1***************tranCount:" + tranCount1);
			
		// 事务绑定
		Trans.exec(Connection.TRANSACTION_REPEATABLE_READ, new Atom() {
			public void run() {
				getBaseDao().execute(sql9);
				int tranCount = sql9.getInt();
				System.out.println("2***************tranCount:" + tranCount);
			}
		});

	}

给你段代码测试,看看Connection对象被调用了什么方法

    @Test
    public void test_conn_call() throws IocException, SQLException {
        DataSource ds = new DataSource() {
            public Connection getConnection() throws SQLException {
                final Connection conn = ioc.get(DataSource.class).getConnection();
                return (Connection)Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[]{Connection.class}, new InvocationHandler() {
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println(method);
                        return method.invoke(conn, args);
                    };
                });
            }
            public <T> T unwrap(Class<T> iface) throws SQLException {
                return null;
            }
            
            public boolean isWrapperFor(Class<?> iface) throws SQLException {
                return false;
            }
            
            public void setLoginTimeout(int seconds) throws SQLException {}
            
            public void setLogWriter(PrintWriter out) throws SQLException {}
            
            public Logger getParentLogger() throws SQLFeatureNotSupportedException {
                return null;
            }
            
            public int getLoginTimeout() throws SQLException {
                return 0;
            }
            
            public PrintWriter getLogWriter() throws SQLException {
                return null;
            }
            
            public Connection getConnection(String username, String password) throws SQLException {
                return null;
            }
        };
        final NutDao _dao = new NutDao(ds);
        _dao.exists("t_user");
        
        System.out.println("==============================");
        Lang.quiteSleep(1000);
        
        Trans.exec(new Atom(){
            public void run() {
                _dao.execute(Sqls.create("select 1"));
            }
        });
    }

==============================
2016-01-22 14:09:48,261 [main] DEBUG org.nutz.ioc.impl.NutIoc - Get 'dataSource'<interface javax.sql.DataSource>
public abstract boolean java.sql.Connection.getAutoCommit() throws java.sql.SQLException
public abstract int java.sql.Connection.getTransactionIsolation() throws java.sql.SQLException
public abstract java.sql.Savepoint java.sql.Connection.setSavepoint() throws java.sql.SQLException
2016-01-22 14:09:48,263 [main] DEBUG org.nutz.dao.impl.sql.run.NutDaoExecutor - select 1
public abstract java.sql.Statement java.sql.Connection.createStatement(int,int) throws java.sql.SQLException
public abstract void java.sql.Connection.commit() throws java.sql.SQLException
public abstract int java.sql.Connection.getTransactionIsolation() throws java.sql.SQLException
public abstract boolean java.sql.Connection.isClosed() throws java.sql.SQLException
public abstract int java.sql.Connection.getTransactionIsolation() throws java.sql.SQLException
public abstract void java.sql.Connection.close() throws java.sql.SQLException

在测试代码上加判断setSavePoint

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println(method);
                        if (method.getName().contains("setSavepoint"))
                        	return null;
                        return method.invoke(conn, args);
                    };

后台打印

DEBUG main - Get 'dataSource'<interface javax.sql.DataSource>
public abstract boolean java.sql.Connection.getAutoCommit() throws java.sql.SQLException
public abstract int java.sql.Connection.getTransactionIsolation() throws java.sql.SQLException
public abstract java.sql.Savepoint java.sql.Connection.setSavepoint() throws java.sql.SQLException
DEBUG main - select 1
public abstract java.sql.Statement java.sql.Connection.createStatement(int,int) throws java.sql.SQLException
public abstract void java.sql.Connection.commit() throws java.sql.SQLException
public abstract int java.sql.Connection.getTransactionIsolation() throws java.sql.SQLException
public abstract boolean java.sql.Connection.isClosed() throws java.sql.SQLException
public abstract int java.sql.Connection.getTransactionIsolation() throws java.sql.SQLException
public abstract void java.sql.Connection.close() throws java.sql.SQLException

改完不是应该看TransCount的值吗?

最新测试代码:

package com;

import java.io.PrintWriter;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;

import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.nutz.dao.Sqls;
import org.nutz.dao.impl.NutDao;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.Ioc;
import org.nutz.ioc.IocException;
import org.nutz.ioc.impl.NutIoc;
import org.nutz.ioc.loader.json.JsonLoader;
import org.nutz.lang.Lang;
import org.nutz.trans.Atom;
import org.nutz.trans.Trans;

public class TestConnect  {

	@Test
    public void test_conn_call() throws IocException, SQLException {
		
		final Ioc ioc = new NutIoc(new
				JsonLoader("datasource.js"));
		
        DataSource ds = new DataSource() {
            public Connection getConnection() throws SQLException {
                final Connection conn = ioc.get(DataSource.class).getConnection();
                return (Connection)Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[]{Connection.class}, new InvocationHandler() {
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    	if (method.getName().contains("setSavepoint"))
                        	return null;
                        System.out.println(method);
                        
                        return method.invoke(conn, args);
                    };
                });
            }
            public <T> T unwrap(Class<T> iface) throws SQLException {
                return null;
            }
            
            public boolean isWrapperFor(Class<?> iface) throws SQLException {
                return false;
            }
            
            public void setLoginTimeout(int seconds) throws SQLException {}
            
            public void setLogWriter(PrintWriter out) throws SQLException {}
            
            public Logger getParentLogger() throws SQLFeatureNotSupportedException {
                return null;
            }
            
            public int getLoginTimeout() throws SQLException {
                return 0;
            }
            
            public PrintWriter getLogWriter() throws SQLException {
                return null;
            }
            
            public Connection getConnection(String username, String password) throws SQLException {
                return null;
            }
        };
        final NutDao _dao = new NutDao(ds);
        _dao.exists("t_user");
        
        System.out.println("==============================");
        Lang.quiteSleep(1000);
        
        
        final String ssql = "select @@@@TranCount ";
		final Sql sql9;
		sql9 = Sqls.create(ssql);
		sql9.setCallback(Sqls.callback.integer());
		
		_dao.execute(sql9);
		int tranCount1 = sql9.getInt();
		System.out.println("1***************tranCount:" + tranCount1);
        
        Trans.exec(new Atom(){
            public void run() {
        		sql9.setCallback(Sqls.callback.integer());        		
        		_dao.execute(sql9);
        		int tranCount1 = sql9.getInt();
        		System.out.println("2***************tranCount:" + tranCount1);
        		
                _dao.execute(Sqls.create("select 1"));
            }
        });
        
        sql9.setCallback(Sqls.callback.integer());        		
		_dao.execute(sql9);
		tranCount1 = sql9.getInt();
		System.out.println("2***************tranCount:" + tranCount1);
		
        _dao.execute(Sqls.create("select 1"));
    }
	
}

后台打印:

DEBUG main - Get 'dataSource'<interface javax.sql.DataSource>
public abstract boolean java.sql.Connection.getAutoCommit() throws java.sql.SQLException
public abstract void java.sql.Connection.setAutoCommit(boolean) throws java.sql.SQLException
DEBUG main - select @@TranCount
public abstract java.sql.Statement java.sql.Connection.createStatement(int,int) throws java.sql.SQLException
public abstract boolean java.sql.Connection.getAutoCommit() throws java.sql.SQLException
public abstract void java.sql.Connection.commit() throws java.sql.SQLException
public abstract boolean java.sql.Connection.getAutoCommit() throws java.sql.SQLException
public abstract void java.sql.Connection.close() throws java.sql.SQLException
1***************tranCount:0
DEBUG main - Get 'dataSource'<interface javax.sql.DataSource>
public abstract boolean java.sql.Connection.getAutoCommit() throws java.sql.SQLException
public abstract int java.sql.Connection.getTransactionIsolation() throws java.sql.SQLException
DEBUG main - select @@TranCount
public abstract java.sql.Statement java.sql.Connection.createStatement(int,int) throws java.sql.SQLException
2***************tranCount:0
DEBUG main - select 1
public abstract java.sql.Statement java.sql.Connection.createStatement(int,int) throws java.sql.SQLException
public abstract void java.sql.Connection.commit() throws java.sql.SQLException
public abstract int java.sql.Connection.getTransactionIsolation() throws java.sql.SQLException
public abstract boolean java.sql.Connection.isClosed() throws java.sql.SQLException
public abstract int java.sql.Connection.getTransactionIsolation() throws java.sql.SQLException
public abstract void java.sql.Connection.close() throws java.sql.SQLException
DEBUG main - Get 'dataSource'<interface javax.sql.DataSource>
public abstract boolean java.sql.Connection.getAutoCommit() throws java.sql.SQLException
public abstract void java.sql.Connection.setAutoCommit(boolean) throws java.sql.SQLException
DEBUG main - select @@TranCount
public abstract java.sql.Statement java.sql.Connection.createStatement(int,int) throws java.sql.SQLException
public abstract boolean java.sql.Connection.getAutoCommit() throws java.sql.SQLException
public abstract void java.sql.Connection.commit() throws java.sql.SQLException
public abstract boolean java.sql.Connection.getAutoCommit() throws java.sql.SQLException
public abstract void java.sql.Connection.close() throws java.sql.SQLException
2***************tranCount:0
DEBUG main - Get 'dataSource'<interface javax.sql.DataSource>
public abstract boolean java.sql.Connection.getAutoCommit() throws java.sql.SQLException
public abstract void java.sql.Connection.setAutoCommit(boolean) throws java.sql.SQLException
DEBUG main - select 1
public abstract java.sql.Statement java.sql.Connection.createStatement(int,int) throws java.sql.SQLException
public abstract boolean java.sql.Connection.getAutoCommit() throws java.sql.SQLException
public abstract void java.sql.Connection.commit() throws java.sql.SQLException
public abstract boolean java.sql.Connection.getAutoCommit() throws java.sql.SQLException
public abstract void java.sql.Connection.close() throws java.sql.SQLException

添加回复
请先登陆
回到顶部