新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论Java, J2SE, J2ME, J2EE, 以及Eclipse, NetBeans, JBuilder等Java开发环境,还有JSP, JavaServlet, JavaBean, EJB以及struts, hibernate, spring, webwork2, Java 3D, JOGL等相关技术。
    [返回] 中文XML论坛 - 专业的XML技术讨论区计算机技术与应用『 Java/Eclipse 』 → spring中使用编程级的事务(转自我的blog) 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 5247 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: spring中使用编程级的事务(转自我的blog) 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     mfc42d 帅哥哟,离线,有人找我吗?
      
      
      等级:大三暑假(ITELS考了6.5分!)(版主)
      文章:65
      积分:882
      门派:XML.ORG.CN
      注册:2004/6/13

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给mfc42d发送一个短消息 把mfc42d加入好友 查看mfc42d的个人资料 搜索mfc42d在『 Java/Eclipse 』的所有贴子 引用回复这个贴子 回复这个贴子 查看mfc42d的博客楼主
    发贴心情 spring中使用编程级的事务(转自我的blog)

    使用org.springframework.transaction.support.TransactionTemplate类,
    TransactionTemplate封装了事务管理的功能,包括异常时的事务回滚,以及操作成
    功后的事务提交。在doInTransaction中进行的操作,如果抛出未捕获异常将被自动回滚,
    如果成功执行,则将被自动提交。使用TransactionTemplate类,必须实现TransactionCallback接口,
    transactionCallback只有一个实现方法,使用匿名内部类,doInTransaction()处理事务。
    类似与JDbcTemplate,Callback类的编写违背很多人的编程习惯,传进匿名类之前要改成final。
    配置文件
    <bean id="transactionTemplate" class="org.springframework.
    transaction.support.TransactionTemplate">

    <property name="transactionManager">

    <ref bean="transactionManager"/>

    </property>

    </bean>

    DAO中
    <property name=" transactionTemplate">

    <ref bean=" transactionTemplate"/>

    </property>

    使用TransactionTemplate的doInTransaction() 不能抛出任何checked exceptions,必须将异常包装为unchecked exceptions,
    因为TransactionTemplate不支持throws
    下面是代码
    transactionTemplate.execute(
    new TransactionCallback() {
    public Object doInTransaction(TransactionStatus ts) {
    try {
    // do stuff
    } catch (Exception e) {
    ts.setRollbackOnly();
    }
    return null;
    }
    }
    );
    使用PlatformTransactionManager效率更高
    package module;

    import org.springframework.transaction.PlatformTransactionManager;
    import org.springframework.transaction.TransactionDefinition;
    import org.springframework.transaction.TransactionStatus;
    import org.springframework.transaction.support.DefaultTransactionDefinition;
    import org.springframework.jdbc.datasource.DriverManagerDataSource;
    import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.dao.DataAccessException;

    import javax.sql.DataSource;

    public class testTranORacleDAO {
        private static DataSource dataSource;
        private static PlatformTransactionManager transactionManager;
        private static JdbcTemplate jt;

        public static void main(String[] args) {
            DriverManagerDataSource dataSource = new DriverManagerDataSource();
            dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
            dataSource.setUrl("jdbc:oracle:thin:@localhost:1521:ILoveMM");
            dataSource.setUsername("system");
            dataSource.setPassword("manager");
            DataSourceTransactionManager tm = new DataSourceTransactionManager();
            tm.setDataSource(dataSource);
            transactionManager = tm;
            doUpdate(dataSource);
        }

        public static void doUpdate(DataSource dataSource) {
            DefaultTransactionDefinition td = new DefaultTransactionDefinition(
                    TransactionDefinition.PROPAGATION_REQUIRED);
            td.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
            td.setTimeout(10);
            TransactionStatus status = transactionManager.getTransaction(td);

            try {
                update(dataSource);
            } catch (DataAccessException e) {
                transactionManager.rollback(status);
                throw e;
            }
            transactionManager.commit(status);
        }

        public static void update(DataSource dataSource) {
            jt = new JdbcTemplate(dataSource);
            jt.update("delete  from beers where id=2 ");
            jt.update("insert into beers(id,BRAND,price) values(1,'3',100)");
        }
    }


       收藏   分享  
    顶(0)
      




    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/2/9 17:17:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 Java/Eclipse 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/6/3 23:29:02

    本主题贴数1,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    4,421.875ms