[转]IBatis连接数据库学习笔记

来源:互联网 发布:cryengine 源码编译 编辑:程序博客网 时间:2024/06/01 22:57

原文地址:http://www.cnblogs.com/lotuswlz/archive/2008/01/10/1034209.html
IBatis连接Oracle连接如下:
<transactionManager type="JDBC">
    <dataSource type="SIMPLE">
      <property name="JDBC.Driver"
   value="oracle.jdbc.driver.OracleDriver"/>
      <property name="JDBC.ConnectionURL"
   value="jdbc:oracle:thin:@localhost:1521:oradb"/>
      <property name="JDBC.Username" value="用户名"/>
      <property name="JDBC.Password" value="密码"/>
      <property name="Pool.MaximumActiveConnections"
   value="10"/>
      <property name="Pool.MaximumIdleConnections" value="5"/>
      <property name="Pool.MaximumCheckoutTime"
   value="120000"/>
      <property name="Pool.TimeToWait" value="500"/>
      <property name="Pool.PingQuery" value="select 1 from
   ACCOUNT"/>
      <property name="Pool.PingEnabled" value="false"/>
      <property name="Pool.PingConnectionsOlderThan"
value="1"/>
      <property name="Pool.PingConnectionsNotUsedFor"
value="1"/>
    </dataSource>
</transactionManager>

当然,这个没什么好说的。


对应段落,连接
Sql Server 2000的如下:

<transactionManager type="JDBC">

    <dataSource type="SIMPLE">
      <property name="JDBC.Driver"
            value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/>
      <property name="JDBC.ConnectionURL"
            value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs;SelectMethod=Cursor"/>
      <property name="JDBC.Username" value="sa"/>
      <property name="JDBC.Password" value=""/>
      <property name="Pool.MaximumActiveConnections"
            value="10"/>
      <property name="Pool.MaximumIdleConnections" value="5"/>
      <property name="Pool.MaximumCheckoutTime"
            value="120000"/>
      <property name="Pool.TimeToWait" value="500"/>
      <property name="Pool.PingQuery" value="select 1 from
            ACCOUNT"/>
      <property name="Pool.PingEnabled" value="false"/>
      <property name="Pool.PingConnectionsOlderThan"
        value="1"/>
      <property name="Pool.PingConnectionsNotUsedFor"
        value="1"/>
    </dataSource>
</transactionManager>

这个值得提到的是:
JDBC.ConnectionURL的值
          
jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs;SelectMethod=Cursor
这里面最后一句:SelectMethod=Cursor,据说是打开了服务器游标,不加默认的是SelectMethod=direct,使用IBatis要求SelectMethod的值必须是Cursor,否则会报错
[Microsoft][SQLServer 2000 Driver for JDBC]Can't start a cloned connection while in manual transaction mode.
有以下解释提供给大家:
If you pass "SelectMethod=cursor" attribute in connectio url, will allow more than one active statement per connection." jdbc:microsoft:sqlserver://localhost:1433;SelectMethod=cursor;","user","pwd"The reason for this is;When SelectMethod is set to direct, SQL Server does not support multiple active statements on a single connection within a transaction; however, when auto-commit mode is enabled (the default), the Microsoft SQL Server JDBC driver provides the ability to have multiple JDBC statements open on a single JDBC connection. This is done by cloning physical SQL Server connections as needed. To avoid cloning physical SQL Server connections in this circumstance, you should create only one Statement, PreparedStatement, CallableStatement, or DatabaseMetaData object per JDBC Connection
object. Be sure to invoke the "close" methods on these objects when you are finished with them, that is, before creating another object of the types described.Sridhar PaladuguMicrosoft Developer SupportJDBC Webdata
在网上看到,有人对这个问题,贴出了微软专家的解释:
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."

不管怎样,当使用IBatis连接sql server2000数据库时,加上SelectMethod=Cursor绝对没问题,但是今天偶然在csdn上看到一个帖子说:

sql server 2005时,用这个又不行了,有必须使用默认支持的SelectMethod=direct,不知道sql server2005的事务机制变成什么样了呢?

原创粉丝点击