1F 利用hibernate执行jdbc中的事物处理 唧唧 Post by : 2008-06-23 11:38:14.0
java代码
-
- try{
- Session session = HibernateUtil.currentSession();
- try{
- Connection connection = session.connection();
- PreparedStatement preparedStatement = null;
- String deleteSQL = "delete from a where b=c";
-
- connection.setAutoCommit(false);
- preparedStatement = connection.prepareStatement(deleteSQL);
- preparedStatement.executeUpdate();
-
- connection.commit();
-
- connection.setAutoCommit(true);
- preparedStatement.close();
- }catch (SQLException x) {
- connection.rollback();
- throw x;
- }
- }catch(HibernateException e){
- e.printStackTrace();
- throw e;
- }finally{
- HibernateUtil.closeSession();
- }
-
-
- try{
- Session session = HibernateUtil.currentSession();
- try{
- Connection conn = session.connection();
- PreparedStatement prepstmt = conn.prepareStatement(query);
- prepstmt.setLong(1,Long.parseLong(paperId));
- ResultSet rs = prepstmt.executeQuery();
- ArrayList rsl = new ArrayList();
- while(rs.next()){
- HashMap item = new HashMap();
- item.put("××",Long.toString(rs.getLong("××")));
- rsl.add(item);
- }
-
- rs.close();
- prepstmt.close();
- }catch (SQLException x) {
- connection.rollback();
- throw x;
- }
- }catch(HibernateException e){
- e.printStackTrace();
- throw e;
- }finally{
- HibernateUtil.closeSession();
- }
-
-
- try{
- Session session = HibernateUtil.currentSession();
- Connection conn = session.connection();
- CallableStatement cstmt = conn.prepareCall("{call SAVE_AS_NEW_PAPER(?,?)}");
- cstmt.setLong(1, new Long(newPid).longValue());
- cstmt.setLong(2, new Long(oldPid).longValue());
- cstmt.executeQuery();
- cstmt.close();
- }catch(HibernateException e){
- throw e;
- }catch(SQLException e){
- throw e;
- }finally{
- HibernateUtil.closeSession();
- }
|