bboss-persistent结合bboss-aop实现注解事务

本文介绍如何使用bboss框架结合AOP实现基于注解的事务管理。通过具体示例展示了不同事务传播级别的配置方法,并提供了测试用例及配置文件。
bboss-persistent结合bboss-aop也可以实现注解事务哦.

先看一个业务组件:

package org.frameworkset.spi.transaction.annotation;

import java.sql.SQLException;

import com.frameworkset.common.poolman.DBUtil;
import com.frameworkset.orm.annotation.RollbackExceptions;
import com.frameworkset.orm.annotation.Transaction;
import com.frameworkset.orm.annotation.TransactionType;
import com.frameworkset.orm.transaction.TransactionManager;

/**
* <p>Title: TXA.java</p>
* <p>Description: </p>
* <p>bboss workgroup</p>
* <p>Copyright (c) 2007</p>
* @Date 2010-1-19 下午05:17:43
* @author biaoping.yin
* @version 1.0
*/
public class TXA
{

@Transaction(TransactionType.REQUIRED_TRANSACTION)
@RollbackExceptions({"java.sql.SQLException","org.frameworkset.spi.transaction.Exception1"})
public void executeTXDBFailed() throws SQLException
{
DBUtil db = new DBUtil();
try
{
System.out.println("executeTXDBFailed:"+TransactionManager.getTransaction());
db.executeInsert("insert into char_table(id) values(2) ");
db.executeDelete("delete ar_table wher id=4");
}
catch (SQLException e)
{
throw e;
}
}

@Transaction
public void executeDefualtTXDB() throws SQLException
{
DBUtil db = new DBUtil();
try
{
db.executeInsert("insert into char_table(id) values(2) ");
db.executeDelete("delete from char_table where id=4");
System.out.println("executeTXDBFailed:"+TransactionManager.getTransaction());
}
catch (SQLException e)
{
throw e;
}
}

@Transaction(TransactionType.REQUIRED_TRANSACTION)
@RollbackExceptions({"java.sql.SQLException","org.frameworkset.spi.transaction.Exception1"})
public void executeTxDB() throws SQLException
{
DBUtil db = new DBUtil();
try
{
db.executeSelect("select * from char_table where id=2");
System.out.println("db.size()="+ db.size());
db.executeInsert("insert into char_table(id) values(3) ");
db.executeInsert("insert into char_table(id) values(4) ");
db.executeDelete("delete from char_table where id=3");
System.out.println("executeTxDB:"+TransactionManager.getTransaction());
}
catch (SQLException e)
{
throw e;
}
}
}


再看一下aop配置文件org/frameworkset/spi/transaction/annotation /annotationtx.xml的内容:

<?xml version="1.0" encoding="UTF-8"?>
<properties>
<property name="annotation.test" singlable="true" class="org.frameworkset.spi.transaction.annotation.TXA"/>
</properties>


再看看测试用例:

package org.frameworkset.spi.transaction.annotation;

import java.sql.SQLException;

import org.frameworkset.spi.ApplicationContext;
import org.junit.Test;

/**
* <p>Title: TestAnnotationTx.java</p>
* <p>Description: </p>
* <p>bboss workgroup</p>
* <p>Copyright (c) 2007</p>
* @Date 2010-1-19 下午05:12:03
* @author biaoping.yin
* @version 1.0
*/
public class TestAnnotationTx
{
ApplicationContext context = ApplicationContext.getApplicationContext("org/frameworkset/spi/transaction/annotation/annotationtx.xml");
@Test
public void testTxfailed()
{
TXA ai = (TXA)context.getBeanObject("annotation.test");
try
{
ai.executeTXDBFailed();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
@Test
public void testTx()
{

TXA ai = (TXA)context.getBeanObject("annotation.test");
try
{
ai.executeTxDB();
}
catch (SQLException e)
{
e.printStackTrace();
}
}

@Test
public void testDefualtTx()
{
TXA ai = (TXA)context.getBeanObject("annotation.test");
try
{
ai.executeDefualtTXDB();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}

可到sourceforge下载测试用例,测试用例包含在最新版本bbossgroups-3.4,下载地址:
[url]http://sourceforge.net/projects/bboss/files/[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值