Some print.
Some print.
~SingleForum~无废话记录~ + ~19~记录点有用的好吗?少说废话少装b~

~SingleForum~无废话记录~ » java

SPRING事务管理的一个例子

Started 2 years ago by admin. | Tags: . .

SPRING事务管理的一个例子

JAVA:
  1. /***
  2.      *
  3.      * 删除TAGS 需要同时删除tree_posts_tags表中的相关信息
  4.      */
  5.     public int removeTagsandPost(final String tagsID) {
  6.  
  7.         String countTags = "";
  8.         final InvocationResult result = new InvocationResult();
  9.  
  10.         DataSourceTransactionManager tran = new DataSourceTransactionManager(
  11.                 getDataSource());
  12.         TransactionTemplate trantemplate = new TransactionTemplate(tran);
  13.  
  14.         countTags = (String) trantemplate.execute(new TransactionCallback() {
  15.             public Object doInTransaction(TransactionStatus status) {
  16.  
  17.                 try {
  18.                     String sqlTags = "delete from tree_tags t where t.t_id = "
  19.                             + NumberUtils.toLong(tagsID);
  20.                     String sqlPostTags = "delete from tree_posts_tags t where t.t_id = "
  21.                             + NumberUtils.toLong(tagsID);
  22.  
  23.                     int count = getJdbcTemplate().update(sqlTags,
  24.                             new Object[] {}, new int[] {});
  25.                     int countPostTags = getJdbcTemplate().update(sqlPostTags,
  26.                             new Object[] {}, new int[] {});
  27.  
  28.                     return String.valueOf(count + countPostTags);
  29.  
  30.                 } catch (TransactionException te) {
  31.                     status.setRollbackOnly();
  32.                     if (result.getException() == null) {
  33.                         throw te;
  34.                     } else {
  35.                     }
  36.                     return 0;
  37.                 }
  38.             }
  39.         });
  40.  
  41.         return NumberUtils.toInt(countTags);

RSS feed for this topic