随笔-39  评论-33  文章-0  trackbacks-0
newxy 新坐标的事务管理
newxy新坐标 技术运用之六
 作者:胡立新
一、简介
newxy(新坐标)可以同时对多个数据库进行事务管理,newxy(新坐标)的事务由类net.newxy.dbm.Transaction来完成。
newxy(新坐标)目前只支持本地事务(在未来版本中,如果数据库连接有JTA的支持,那么在newxy(新坐标)事务中进行的操作将是整个原子性JTA事务的一部分)。
        一个Transaction实例除有一个主线程外,还有一个专门负责超时回滚任务的线程。主线程负责对一批需要一次性完成的单元进行操作。如果在设定或默认的时间内主线程一批操作尚未完成,负责超时回滚任务的线程会干预,回滚事务。
    newxy(新坐标)的事务管理很方便,只需在调用IFacade接口方法前调用事务方法call(IFacade ifacade), 或方法call(IFacade ifacade,int transactionIsolation),如 tran.call(ifacade).update(dto)
 
二、运用举例:
设有数据库有两表:
/*客户表*/
create table customers (
    id int primary key,
    name VARCHAR(255)
)
/*订单表*/
create table orders (
    id int primary key,
    customer_id int,
    date datetime
)
运用一
//新建一名叫“张五”的客户,新建一与此用户关联的定单,订单表字段customer_id是“张五”客户的id号,
//id号由系统自动生成。
package common;
 
import net.newxy.dbm.*;
import org.apache.commons.beanutils.DynaBean;
 
public class Test{
    public static void main(String[] args) {
        TestSqlServerDao dao1=new TestSqlServerDao();
 
        DynaDto customerDto=new DynaDto();
        customerDto.set_table("customers");
        customerDto.set("name","张五");
 
        DynaDto ordersDto=new DynaDto();
        ordersDto.set_table("orders");
        ordersDto.set("date",net.newxy.util.DateTools.todayInTs());
 
        Transaction tran=new Transaction();
        try{
            tran.begin();
            Object result1=tran.call(dao1).update(customerDto);
            if(result1!=null){
                // result不等于空,表明是插入操作,且result中包含了自动生成的主关键字值。
                ordersDto.set("customer_id",((DynaBean)result1).get("id"));
            }else{
                // result为空,表明是update操作,客户id保存在customerDto中。
                // 此例因为没有给customerDto设置id,因而肯定是自动获得id值后作插入操作,
                // result肯定不为空。
                ordersDto.set("customer_id",customerDto.get("id"));
            }
            tran.call(dao1).update(ordersDto);
            tran.commit();
        }catch(Exception e2){
            try {
                tran.rollback();
            } catch (Exception ex) {
            }
            System.out.println(e2.getMessage());
        }
    }
}
 
//daoTestSqlServerDao
package common;
 
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import net.newxy.dbm.BaseDAO;
 
public class TestSqlServerDao extends BaseDAO{
    public TestSqlServerDao(){
        super();
}
    public Connection getConnection(String dsJndi) throws Exception {
        Connection cn=null;
        try {
            Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
            cn = DriverManager.getConnection(
                        "jdbc:microsoft:sqlserver://localhost:1433; SendStringParametersAsUnicode=false","webUser","myPass");
        } catch (ClassNotFoundException ex) {
        } catch (IllegalAccessException ex) {
        } catch (InstantiationException ex) {
        } catch (SQLException ex1) {
            throw new Exception(ex1.getMessage());
        }
        return cn;
    }
}
 
运用二
    //默认的60秒后,负责超时回滚任务的线程将检查事务是否已完成,如果没完成,回滚事务。
    //设置超时时间的方法是:
        Transaction tran=new Transaction();
        tran.setTransactionTimeout(10);
        try{
            tran.begin();
            ......
            tran.commit();
        }catch(Exception e2){
            try {
                tran.rollback();
            } catch (Exception ex) {
            }
            System.out.println(e2.getMessage());
        }
 
运用三
    //设置事务隔离等级:
        Transaction tran=new Transaction();
        tran.setTransactionIsolation(java.sql.Connection.TRANSACTION_READ_UNCOMMITTED);
        try{
            tran.begin();
            ......
            tran.commit();
        }catch(Exception e2){
            try {
                tran.rollback();
            } catch (Exception ex) {
            }
            System.out.println(e2.getMessage());
        }
 
运用四
    // 事务同时对多个数据库操作。方法是,如同daoTestSqlServerDao,再建另一dao类Dao2,
    // Dao2实现的getConnection(String dsJndi)方法获得另一数据库连接。
 
        TestSqlServerDao dao1=new TestSqlServerDao();
        Dao2 dao2=new Dao2();
 
        Transaction tran=new Transaction();
        try{
            tran.begin();
            ......
            tran.call(dao1,).update(...);
            tran.call(dao2).remove(...);
 
            ......
            tran.commit();
        }catch(Exception e2){
            try {
                tran.rollback();
            } catch (Exception ex) {
            }
            System.out.println(e2.getMessage());
        }
 
运用五
    //事务中开发者自己要直接运用数据库连接,但开发者不能在事务中关闭数据库连接。
        TestSqlServerDao dao1=new TestSqlServerDao();
        Transaction tran=new Transaction();
        try{
            tran.begin();
            ......
            Connection con=tran.call(dao1).getConnection();
            ......
            tran.commit();
        }catch(Exception e2){
            try {
                tran.rollback();
            } catch (Exception ex) {
            }
            System.out.println(e2.getMessage());
        }
 
运用六
    // 事务同时对多个数据库操作,且不同数据库要求不同的隔离等级,
 
        TestSqlServerDao dao1=new TestSqlServerDao();
        Dao2 dao2=new Dao2();
 
        Transaction tran=new Transaction();
        try{
            tran.begin();
            ......
            tran.call(dao1,java.sql.Connection.TRANSACTION_READ_UNCOMMITTED).update(...);
            tran.call(dao2,java.sql.Connection.TRANSACTION_NONE)).remove(...);
            ......
            tran.commit();
        }catch(Exception e2){
            try {
                tran.rollback();
            } catch (Exception ex) {
            }
            System.out.println(e2.getMessage());
        }
 
newxy ( 新坐标 ) 技术网站: http://www.newxy.net
posted on 2006-08-15 17:30 newxy新坐标 阅读(400) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: