JDBC rollback failed, Hibernate出错,求解!求解!
bobre 2007-07-09 05:56:06 我是一名新手,最近用struts+hibernate+tomcat写了一个简单的程序,有登录功能。
可如果服务器一天没关,到了第二天,或者是手动修改一下系统日期,服务器就是报以下错误:
org.hibernate.TransactionException: JDBC rollback failed
org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:170)
cg.DBOperate.getUser(DBOperate.java:31)
cg.LoginAction.execute(LoginAction.java:28)
下面把代码简单写一下
=======hibernate.cfg.xml=================
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">pwd</property>
<property name="hibernate.connection.pool_size">100</property>
<property name="show_sql">false</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Mapping files -->
<mapping resource="model.hbm.xml"/>
</session-factory>
</hibernate-configuration>
===========================LoginAction.java==========================
public final class LoginAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
DynaActionForm loginForm = (DynaActionForm) form;
String name = (String)loginForm.get("name");
String pwd = (String)loginForm.get("pwd");
User user = new DBOperate().getUser(name);
ActionMessages messages = new ActionMessages();
if (user == null) {
ActionMessage errors = new ActionMessage(
"LoginAction.fail.username", name);
messages.add("username", errors);
saveErrors(request, messages);
return mapping.findForward("fail");
}
String dbPassword = user.getPwd();
if (dbPassword == null || !dbPassword.equals(pwd)) {
ActionMessage errors = new ActionMessage(
"LoginAction.fail.password");
messages.add("password", errors);
saveErrors(request, messages);
return mapping.findForward("fail");
}
Organ organ = user.getOrgan();
HttpSession session = request.getSession();
session.setAttribute(Constants.LOGIN_USER_KEY, user); session.setAttribute(Constants.USER_ORGAN_KEY, organ);
return (mapping.findForward("successed"));
}
}
========================DBOperate.java===============================
public User getUser(String userId) throws HibernateException {
Session session = HibernateUtil.currentSession();
User user = null;
System.out.println(">>DbOperate.getUser");
Transaction tx = null;
try {
tx = session.beginTransaction();
Query q = session.createQuery("from User where name=:userId");
q.setParameter("userId", userId);
List list = q.list();
if (!list.isEmpty())
user = (User) list.get(0);
tx.commit();
} catch (HibernateException e) {
if (tx != null)
tx.rollback();
throw e;
}
session.close();
return user;
}
=====================HibernateUtil.java==============================
public class HibernateUtil {
private static Log log = LogFactory.getLog(HibernateUtil.class);
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
ex.printStackTrace();
//log.error("Initial SessionFactory creation failed.", ex);
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null||!s.isOpen()) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
}
=============================
提示的错误是在DBOperate.java里的tx.rollback();
这个问题已经困扰我好一个星期,请哪位知道的大侠帮忙解决一下。先谢谢了~~