用SQL2000+Tomcat5+Hibernate的时候,出现以下数据库事务错误,是为什么?请看代码

03-12-30 interbase
Could not load object: [Microsoft][SQLServer 2000 Driver for JDBC]Can't start a cloned connection while in manual transaction mode.

调用beginTransaction()时错误出现以上错误,是为什么?



package com.hellking.study.hibernate;

import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
import java.util.*;
import java.io.IOException;
import java.io.PrintWriter;

public abstract class HibernateBase
{
protected SessionFactory sessionFactory;//会话工厂,用于创建会话
protected Session session;//hibernate会话
protected Transaction transaction; //hiberante事务

public HibernateBase()throws HibernateException
{
this.initHibernate();
}
// 帮助方法
protected void initHibernate()
throws HibernateException {

// 装载配置,构造SessionFactory对象
sessionFactory = new Configuration().configure().buildSessionFactory();
}

/**
*开始一个hibernate事务
*/
protected void beginTransaction()
throws HibernateException {

session = sessionFactory.openSession();
transaction = session.beginTransaction();
}

/**
*结束一个hibernate事务。
*/
protected void endTransaction(boolean commit)
throws HibernateException {

if (commit) {
transaction.commit();
} else {
//如果是只读的操作,不需要commit这个事务。
transaction.rollback();
}
session.close();
}
}


调用时错误:


package com.hellking.study.hibernate;

import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
import java.util.*;

/**
*和course相关的业务逻辑
*/
public class CourseBean extends HibernateBase
{

public CourseBean()throws HibernateException
{
super();
}
/**
*增加一个Course
*/
public void addCourse(Course st)throws HibernateException
{
beginTransaction();
session.save(st);
endTransaction(true);
}

/**
*返回系统中所有的Course
*/
public Iterator getAllCourses()throws HibernateException
{
String queryString = "select courses from Course as courses";
beginTransaction();
Query query = session.createQuery(queryString);
Iterator it= query.iterate();
return it;
}

/**
*删除给定ID的course
*/
public void deleteCourse(String id)throws HibernateException
{
beginTransaction();
Course course=(Course)session.load(Course.class,id);
session.delete(course);
endTransaction(true);
}

/**
*按course的名字进行模糊查找
*/
public Iterator getSomeCourse(String name)throws HibernateException
{
String queryString = "select c from Course as c where c.name like :name" ;
beginTransaction();
Query query = session.createQuery(queryString);
query.setString("name", "%"+name+"%");
Iterator it= query.iterate();
return it;
}

}

Jerry_ypy
2004-03-30 18:27
[Microsoft][SQLServer 2000 Driver for JDBC]Can't start a cloned connection while in manual transaction mode.

在同一个connection中对一个表先进行了读操作,然后进行写操作,就会出现这样的问题。

easyinfonet
2004-04-01 14:26
jdbc包的问题
如果写一个错误sql(可以激活rollback)
就会报这个错误

flashroom
2004-07-27 09:07
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

======================================
哈哈哈哈,找了好长时间我才找到解决办法,哈哈哈哈哈哈