我直接帖代码吧
public void batchUpdateXfen(String sql, List<XfenStats> list) {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
Connection con= session.connection();
PreparedStatement stmt = null;
int count=0;
try {
con.setAutoCommit(false);
stmt = con.prepareStatement(sql);
for(int i=0;i<list.size();i++){
count++;
stmt.setDouble(1,list.get(i).getSocre());
stmt.setString(2,list.get(i).getId());
stmt.addBatch();
if(count%500==0){
stmt.executeBatch();
con.commit();
stmt.clearBatch();
}
}
if(count%500!=0){
stmt.executeBatch();
con.commit();
stmt.clearBatch();
}
} catch (SQLException e) {
e.printStackTrace();
try {
con.rollback();
} catch (SQLException e1) {
e1.printStackTrace();}
}finally{try {if(stmt!=null){
stmt.close();}}
} catch (SQLException e) {e.printStackTrace();}}}
--
这里执行不报任何异常,但就是偶尔数据库里不更新数据。
找了很久也没发现问题。。。
是因为事务引起的吗? 配置文件里已经为hibernae 配了事物管理了。
开始session 是Session session = getHibernateTemplate().getSessionFactory().openSession();获取的。所以有时候不执行
后来改了,本地没问题。
但同事那还是有问题,。代码已经同步了。