ansaction not successfully started 处理办法
来源:互联网 发布:马克安东尼 知乎 编辑:程序博客网 时间:2024/06/05 04:38
Transaction not successfully started 处理办法
代码如下
public User valid(String username, String password)
throws HibernateException {
Session session = null;
Transaction tx = null;
User user = null;
try {
session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
Query query = session
.createQuery("from User where username=? and password=?");
query.setString(0, username.trim());
query.setString(1, password.trim());
user = (User) query.uniqueResult();
query = null;
tx.commit();
} catch (HibernateException e) {
throw e;
} finally {
if (tx != null) {
tx.rollback();
}
HibernateSessionFactory.closeSession();
}
return user;
}
Exception in thread "main" org.hibernate.TransactionException: Transaction not successfully started
at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:149)
at com.hibernate.dao.UserDAO.valid(UserDAO.java:38)
at com.hibernate.dao.UserDAO.main(UserDAO.java:104)
分析原因:个人认为应该讲if()部分代码放在catch()块中在throw之前。因为抛出异常后就不应该再有其他语句。所以将红色部分代码修改为:
catch (HibernateException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
HibernateSessionFactory.closeSession();
}
下面为修改后的运行结果:
Hibernate: select user0_.id as id0_, user0_.username as username0_, user0_.password as password0_, user0_.email as email0_ from user user0_ where user0_.username=?
false
Hibernate: select user0_.id as id0_, user0_.username as username0_, user0_.password as password0_, user0_.email as email0_ from user user0_ where user0_.username=?
false
Hibernate: insert into user (username, password, email) values (?, ?, ?)
true
显然修改后异常没有了,编译顺利通过,也得到了想要的效果
代码如下
public User valid(String username, String password)
throws HibernateException {
Session session = null;
Transaction tx = null;
User user = null;
try {
session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
Query query = session
.createQuery("from User where username=? and password=?");
query.setString(0, username.trim());
query.setString(1, password.trim());
user = (User) query.uniqueResult();
query = null;
tx.commit();
} catch (HibernateException e) {
throw e;
} finally {
if (tx != null) {
tx.rollback();
}
HibernateSessionFactory.closeSession();
}
return user;
}
Exception in thread "main" org.hibernate.TransactionException: Transaction not successfully started
at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:149)
at com.hibernate.dao.UserDAO.valid(UserDAO.java:38)
at com.hibernate.dao.UserDAO.main(UserDAO.java:104)
分析原因:个人认为应该讲if()部分代码放在catch()块中在throw之前。因为抛出异常后就不应该再有其他语句。所以将红色部分代码修改为:
catch (HibernateException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
HibernateSessionFactory.closeSession();
}
下面为修改后的运行结果:
Hibernate: select user0_.id as id0_, user0_.username as username0_, user0_.password as password0_, user0_.email as email0_ from user user0_ where user0_.username=?
false
Hibernate: select user0_.id as id0_, user0_.username as username0_, user0_.password as password0_, user0_.email as email0_ from user user0_ where user0_.username=?
false
Hibernate: insert into user (username, password, email) values (?, ?, ?)
true
显然修改后异常没有了,编译顺利通过,也得到了想要的效果
- ansaction not successfully started 处理办法
- Transaction not successfully started 处理办法
- 有关Transaction not successfully started问题解决办法
- Transaction not successfully started
- Transaction not successfully started
- Transaction not successfully started
- Transaction not successfully started 异常的解决
- org.hibernate.TransactionException: Transaction not successfully started
- org.hibernate.TransactionException: Transaction not successfully started
- 出现org.hibernate.TransactionException: Transaction not successfully started的原因
- hibernate提示错误的原因Transaction not successfully started
- Hibernate 错误解决(TransactionException: Transaction not successfully started)
- 出现org.hibernate.TransactionException: Transaction not successfully started
- daemon started successfully
- spring管理hibernate事务报异常--Transaction not successfully started解决方法
- spring管理hibernate事务报异常--Transaction not successfully started解决方法
- hibernate 事务嵌套导致org.hibernate.TransactionException: Transaction not successfully started
- spring管理hibernate事务报异常--Transaction not successfully started解决方法
- [黑马程序员]css选择器总结
- HDLC协议介绍
- 迪杰斯特拉算法(可打印最短路径)
- 无线网络连接之故障解析
- table 列表自动滚动显示代码
- ansaction not successfully started 处理办法
- 内核信息打印在屏幕上
- poj 3207 Ikki's Story IV - Panda's Trick 2-sat+建图
- 使用CREATE VIEW创建视图
- 自写的栈的一些基本处理
- Google、Facebook等技术发展历程
- hibernate与DB的映射问题
- 关于maya拓扑
- 杰出人士的六个好习惯