`

IBATIS 配置报错。解决Can't start a cloned connection while in manual transaction mode问题

阅读更多
实际上你如果在IBATIS入门(第一节)不仔细看我的conn.properties的配置的话,你不会察觉到。

#sql200

user=sa
pwd=sa
driver=com.microsoft.jdbc.sqlserver.SQLServerDriver
url=jdbc:microsoft:sqlserver://localhost:1433;databasename=ibatisetest
如果你这样写,看似没有任何错误,但是他会爆出一下这个错误。
com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in com/dk/user/User.xml.
--- The error occurred while applying a result map.
--- Check the selectUser-AutoResultMap.
--- The error happened while setting a property on the result object.
--- Cause: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Can't start a cloned connection while in manual transaction mode.
怎么办呢?
数据库连接串加上SelectMethod=cursor
向这样
jdbc:microsoft:sqlserver://localhost:1433;SelectMethod=cursor;databasename=ibatisetest
就可以解决了。
解释一下:
SelectMethod=cursor
作用:以利用服务器端的游标加快速度
使用情况:
1.执行多个Statements的操作的时候用
2.需要手动使用事务的时候使用
一下是转载的:【转】:http://looxiaohu.iteye.com/blog/235840
以上是在使用sqlserver数据库的连接字符串的时候使用过。
例如:jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=071008_03;SelectMethod=cursor
但是今天我将其用在oracle数据库的时候,却报出了一下的错误信息:
Io 异常: Connection refused(DESCRIPTION=(TMP=)(VSNNUM=169869824)(ERR=12505)(ERROR_STACK=(ERROR=(CODE=12505)(EMFI=4))))
这是个老话题,2002年在使用ejb的bmp就遇到了这个问题
连接数据库成功之后,想在一个事务中初始化多个预处理句柄时报错
dbConn.setAutoCommit(false)
for (int i = 0; i < 5; i++) {
pstmt[i] = dbConn.prepareStatement(strPreSQL[i]);
错误提示:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Can't start manual transaction mode because there are cloned connections

怀疑MS SQL不能在一个事务中建多个预处理句柄
Resolution:
You have to add a property to the pool definition, something to do with selectMode=cursor or selectMethod=cursor. Check the driver documentation. Otherwise the driver will not allow more than one statement per connection at any given time
微软的专家告诉的
This error occurs when you try to execute multiple statements against a SQL Server database with the JDBC driver while in manual transaction mode (AutoCommit=false) and while using the direct (SelectMethod=direct) mode. Direct mode is the default mode for the driver."
这段翻译:
这是个老话题,2002年在使用ejb的bmp就遇到了这个问题
连接数据库成功之后,想在一个事务中初始化多个预处理句柄时报错
dbConn.setAutoCommit(假)
为(int i = 0;我<5;我+ +)(
pstmt的[我] = dbConn.prepareStatement(strPreSQL [我]);
错误提示:
java.sql.SQLException:[微软] [SQLSERVER的2000年为JDBC]驱动程序不能启动手动交易模式,因为有克隆连接

怀疑MS SQL不能在一个事务中建多个预处理句柄
决议:
你必须添加一个属性到池中的定义,是与selectMode的SelectMethod = =光标或光标。 检查驱动程序文件。 否则,驱动程序将不允许超过每一个声明在任何特定时间连接
微软的专家告诉的
此错误发生在您尝试执行一个SQL Server与反对的JDBC驱动程序数据库多条语句,而在手动交易模式(自动提交= false)并且使用直接(SelectMethod =直接)模式。 直接模式是该驱动程序的默认模式。


import java.sql.*;
import java.io.*;

public class Repro{

public static void main(String args[])
{
try {
Connection con;
Statement s1 = null;
ResultSet r1 = null;
Statement s2 = null;
ResultSet r2 = null;
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con = DriverManager.getConnection(
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs;SelectMethod=Direct;User=User;Password=Password");
//fix 1
//"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs;SelectMethod=Cursor;User=User;Password=Password");
con.setAutoCommit(false);

try {
s1 = con.createStatement();
r1 = s1.executeQuery("SELECT * FROM authors");

//fix 2
//r1.close();
//s1.close();

s2 = con.createStatement();
r2 = s2.executeQuery("SELECT * FROM publishers");
}
catch (SQLException ex)
{
System.out.println(ex);
}

}
catch (Exception e)
{
e.printStackTrace();
}
}
}
用SQL Server驱动一次select很多数据最好在connection string中加上SelectMethod=Cursor,以利用服务器端游标加快速度,其实不只sqlserver,oracle的jdbc,只要使用PreparedStatement,驱动默认就使用游标,sqlserver则不然,必须使用SelectMethod=Cursor才打开游标。
这点在使用jotm时,并且使用Xapool时,必须修改DataSourceFactory,把PreparedStatementPool禁掉,否则记录插的太快了,很可能是游标没来得及关闭
即使不使用jotm,大量向oracle插入数据,例如每毫秒1条,也会引发游标用完,所以大量插入数据时,应该使用oracle的批处理batchupdate.
可惜的是,微软的sqlserver的jdbc驱动不支持这个属性


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics