[code="java"]
public void deleteMoreFlowCards(String[] vouNos) {
String tempVouNos = VouNoGenerator.getString(vouNos, ",");
String sql = "delete from t_flow_card where vouNo in("+tempVouNos+")";
String flowCardDetailSql = "delete from t_flow_card_detail where vou_no in("+tempVouNos+")";
[color=red]此处使用getCurrentSession,就抱Transaction not successfully started
openSession和getCurrentSession的区别是,getCurrentSession提交后,session会自动关闭
而openSession不会,需手动提交,可和这没关系啊。[/color]
Session session = getHibernateTemplate().getSessionFactory().openSession();
//Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
//SQLQuery sqlQuery = session.createSQLQuery(sql);
Connection conn = null;
PreparedStatement psmt=null;
Transaction tx = null;
try {
//conn.setAutoCommit(false);
tx = session.beginTransaction();
conn = session.connection();
psmt = conn.prepareStatement(flowCardDetailSql);
psmt.executeUpdate();
psmt = conn.prepareStatement(sql);
psmt.executeUpdate();
System.out.println("commit"+conn.getAutoCommit());
//conn.commit();
tx.commit();
} catch (SQLException e) {
e.printStackTrace();
if(tx != null){
tx.rollback();
}
/*if(conn != null){
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}*/
}finally{
try {
//conn.setAutoCommit(true);
psmt.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//sqlQuery.executeUpdate();
}
[/code]
描述见红色字体
补充:Hibernate 使用jdbc进行批量删除时,事务管理是使用Connection来管理还是使用Hibernate封装好的Transaction来管理呢?请给建议。谢谢。
字不变红,再补:
[color=red]此处使用getCurrentSession,就抱Transaction not successfully started
openSession和getCurrentSession的区别是,getCurrentSession提交后,session会自动关闭
而openSession不会,需手动提交,可和这没关系啊。[/color]
Session session = getHibernateTemplate().getSessionFactory().openSession();
[b]问题补充:[/b]
[color=red]
getCurrentSession就指得到当前可用的session,如果压根就没开启事务,
[/color]
我事务管理采用的spring的事务管理,不要我手动开啊,
[color=red]
getCurrentSession就指得到当前可用的session
[/color],如果没有会开新的吧