编程式事务
<bean id="dbAtcBean" class="com.utils.DbAtcBean">
<property name="txManager" ref="txManager" />
<property name="txDefinition">
<bean
class="org.springframework.transaction.support.DefaultTransactionDefinition">
<property name="propagationBehaviorName" value="PROPAGATION_REQUIRES_NEW" />
</bean>
</property>
</bean>
try {
dbAtcBean.beginwork(ctx);
insertCdDtl(ctx);
} catch (Exception e) {
dbAtcBean.rollBackWork(ctx);
e.printStackTrace();
}
dbAtcBean.commitwork(ctx);
/**
* 数据库事务管理bean
* @author Administrator
*
*/
public class DbAtcBean {
private String TRANSACTIONSTATUS = "TRANSACTIONSTATUS";
private TransactionDefinition txDefinition;
private PlatformTransactionManager txManager;
public TransactionDefinition getTxDefinition() {
return txDefinition;
}
public void setTxDefinition(TransactionDefinition txDefinition) {
this.txDefinition = txDefinition;
}
public PlatformTransactionManager getTxManager() {
return txManager;
}
public void setTxManager(PlatformTransactionManager txManager) {
this.txManager = txManager;
}
public void commitwork(Context cxt) {
TransactionStatus aa = cxt.getVariable(TRANSACTIONSTATUS);
if(null!=aa && !aa.isCompleted()){
txManager.commit(aa);
}
}
public void rollBackWork(Context cxt) {
TransactionStatus aa = cxt.getVariable(TRANSACTIONSTATUS);
if(null!=aa && !aa.isCompleted()){
txManager.rollback(aa);
}
}
public void beginwork(Context cxt) {
TransactionStatus aa = txManager.getTransaction(txDefinition);
cxt.setVariable(TRANSACTIONSTATUS, aa);
}
}