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,下載下傳位址:
http://sourceforge.net/projects/bboss/files/