分享

JDBC连SQL SERVER数据库常见问题Can't start a cloned connection while

 韵朵之家 2009-05-08

--问题1:出现ClassNotFoundException,这个问题是由于你没有把driver类放到你的classpath中,也就是说
你的程序找不到驱动类,包括三个包:msutil.jar,msbase.jar,mssqlserver.jar

--解决:jb:可以在工程属性中加入这三个包,netbean:可以把这三个包copy到某一个 lib下,也就是某个类的公共库中

--问题2:出现[Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket,这个错误,不论在什么容器下,凡是出现这种错误的,都可以说不是程序本身的错误,因为大部分人都知道:
jdbc:microsoft:sqlserver://localhost:1433;这样写是对的,但为什么这样写呢
######################################################################
如何用jdbc连接到sqlserver2000(翻译英文原裆)(1)
介绍了连接server2000的方法之一:使用DriverManager.getConnection(...)连接到数据库有两种方法,one is :with a connection url through the jdbc driver manager,another is with a jndi datasource.

我们先说第一种:使用DriverManager.getConnection()方法
第一:你需要设置classpath,它是你得jvm加载类时所要寻找的路径。
当然首先你要有jdbs driver for sqlserver。
设置环境变量:window os:set classpath=.;${your path to save the driver}/msbase.jar;${}/mssqlserver.jar;${}/msutil.jar
第二:注册你的驱动registering the driver
注册驱动告诉你得jdbc管理器去加载什么驱动程序类,
当使用class.forName(),你必须指定驱动类名称,
com.microsoft.jdbc.sqlserver.SQLServerDriver.例如:Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
第三:在注册完一个驱动类后,你必须把你的数据库的物理信息以一个url的形式传给管理器,下面是一个url模版:jdbc:microsoft:sqlserver://server_name:port,其中server2000默认的端口号为:1433.下面是一个例子:
Connection con=DriverManager.getConnection("jdbc:microsoft:sqlserver://server_name:1433","userName","password");
注:这里的server_name必须是一个ip address,或者一个主机名,你可以使用ping命令来测试你的主机名是否传回一个正确的ip address
下面是 连接url的属性解释:
------------------------------------------------------------------------------
---属性----------------------------------------说明--------------
--DatabaseName(0)                   ------the name of the sql server database to which you want to connect
--HostProcess (O)                    ------the process ID of the application connecting to SQL Server 2000.The supplied value
                                    ------appears in the "hostprocess" column of the sysprocesses table.
--NetAddress(O)                    ------the MAC address of the network interface card of the application connnecting to sql                                           -----server 2000.the supplied value appears in the "net_address" column of the sysprocesses                                      ------table.
--Password                         ------the case-insensitive password used to connect to your sql server database.
--PortNumber(O)                    ------the tcp 端口号(一般在使用datasource时使用)默认为:1433
--ProgramName(0)                   ------the name of the application connecting to sql server 2000,the supplied value appears in                                      ------the "program_name" column of the sysprocesses table
--SelectMethod                     ------SelectMethod={cursor|direct}.determines wherther database cursors are used for select                                        ------statements .performance and behavior of the driver are affected by the selectMethod                                          -------setting.Direct:->the direct method sends the complete results set in one request to the
                              ----driver .it is useful for queries that only produce a small amount of data that you fetch                              -----completely,you should avoid using direct when executing queries that produce a large amount of                              ----data,as the result set is cached completely on the client and constrains memory .In this mode,each
-------------------------->statement requires its connection to the database.this is accomplished by cloing connections .cloned ------------------------->connections use the same connection properties as the original connection;however,because transactions ------------------->occur on a single connection,auto commit mode is required,Due to this,JTA is not supported in direct mode.In
------------------>addition,some operations,such as updating an insensitive result set,are not supported in direct mode because ---------->driver create a second statement internally.Exceptions generated due to the creation of cloned statements usually return an error message similar to "Cannot start a cloned connection while in manual transaction mode"
----------------------------->Cursor:
--------------------->when the selectMode is set to cursor ,a server-side cursor is generated .行被一块的方式提取出来,JDBC语句
-----------------setFetchSize这时候就起作用了,它可以控制每次提取的行数,cursor值在查询结果产生大量数据的时候非常有用,并且也用在
-------------〉jta中,但是setFetchSize具体设置多大,这是没有什么规则的,你必须多多尝试!
###################################
--解决:启动你的sqlserver2000的服务器网络实用工具后,确保你的Tcp/Ip协议已启动,默认的应该都启动了,这是进行通讯的条件一
然后,在选中Tcp/Ip协议后点击属性,就看到了一个默认端口号,这就是你在 getConnection里用到的端口号,你必须把你程序里用
到的端口号,写成这里的值,这样才能解决上面的问题,当然你也可以在这里把端口号给该了,而保持程序不变!1433

--问题3:有些时候也连接上了,但就是不能访问,或者提示说sql语句有错误
--解决:这些都是管理sqlServer2000所需要注意的,或许你没有给这个用户分配足够的权限,或者你的sql语句中用到了sqlserver里保留的
关键字,我就遇到了这样一个问题:我写了个 select * from USER这个语句怎么执行都不对,后来看了分析器,才知道这是一个关键字,你
不可以用它来命名的!

错误:java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Can't start a cloned connection while in manual transaction mode.
A: 这个错误产生的原因一般是当你在一个SQL SERVER的JDBC连接上执行多个STATEMENTS的操作,或者是手动事务状态(AutoCommit=false) 并且使用 direct (SelectMethod=direct) 模式. Direct 模式是默认的模式.
解决办法
当你使用手动事务模式时,必须把SelectMethod 属性的值设置为 Cursor, 或者是确保在你的连接上只有一个STATEMENT操作

java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Can't start a cloned connection while in manual transaction

mode.

错误:java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Can't start a cloned connection while in manual

transaction mode.
A: 这个错误产生的原因一般是当你在一个SQL SERVER的JDBC连接上执行多个STATEMENTS的操作,或者是手动事务状态(AutoCommit=false) 并

且使用 direct (SelectMethod=direct) 模式. Direct 模式是默认的模式.
解决办法
当你使用手动事务模式时,必须把SelectMethod 属性的值设置为 Cursor, 或者是确保在你的连接上只有一个STATEMENT操作。
一个事务里只能有一个CONNECTION和一个STATEMENT

 

PRB: Cannot Start a Cloned Connection While in Manual Transaction Mode
View products that this article applies to.
Article ID : 313181
Last Review : June 30, 2003
Revision : 2.0
This article was previously published under Q313181
On This Page

 


Steps to Reproduce the Behavior

REFERENCES
SYMPTOMS
While using the Microsoft SQL Server 2000 Driver for JDBC, you may experience the following exception:

java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Can't start a cloned connection while in manual transaction

mode.
Back to the top

CAUSE
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.
Back to the top

RESOLUTION
When you use manual transaction mode, you must set the SelectMethod property of the driver to Cursor, or make sure that you

use only one active statement on each connection as specified in the "More Information" section of this article.
Back to the top

STATUS
This behavior is by design.
Back to the top

 

使用 sqlserver 的 手动事务处理,实现多条sql语句的顺序执行,当遇到操作失败时能够数据回滚。

在我使用 过程中发现了java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Can't start a cloned connection while

in manual transaction mode. 错误。

解决办法是将:数据库连接语句加上 “SelectMethod=cursor ”

例:String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=enterprise;SelectMethod=cursor";

例:String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=enterprise;SelectMethod=cursor";

    public void transSet() throws SQLException {
        flag_conn = true;
        conn = DriverManager.getConnection(url, user, password);
        conn.setAutoCommit(false);

    }

手动事务处理代码:

    public void transCommit() throws SQLException {
        conn.commit();// 提交jdbc事务
        conn.setAutoCommit(true);// 恢复jdbc事务的默认提交方式
        flag_conn = false;
        conn.close();
    }

    public void transRollback() throws SQLException {
        conn.rollback();// 回滚事务
        flag_conn = false;
        conn.close();

    }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多