用SQL2000+Tomcat5+Hibernate的時候,出現以下資料庫事務錯誤,是為什麼?請看程式碼

interbase發表於2003-12-30
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;
}

}

相關文章