最新测试代码:
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