Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 冲浪宝典 > 网络资源 > newxy新坐标的事务管理
【标  题】:newxy新坐标的事务管理
【关键字】:newxy
【来  源】:http://blog.csdn.net/nlhlx/archive/2006/08/11/1052252.aspx

newxy新坐标的事务管理

Your Ad Here
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
 
 
VC中使用ADO进行数据库开发的一些资料的整理:【上一篇】
3D游戏从入门到精通-9:【下一篇】
【相关文章】
  • struts+newxy(新坐标)文件上传,比jspsmart更简单 作者:胡立新
  • struts+hibernate的替代方案:struts+newxy,开发效率提高十倍
  • newxy+struts WEB开发与deiphi桌面开发相比,速度更快,能力更强
  • newxy+struts实现零java代码或极少java代码开发以数据为中心的web运用系统 作者:胡立新
  • newxy(新座标)技术文档 作者:胡立新
  • newxy技术零java代码实现文件下载,下载记数 作者:胡立新
  • newxy技术零java代码实现数据分页显示 作者:胡立新
  • newxy+struts实现零java代码或极少java代码开发以数据为中心的web运用系统
  • 【随机文章】
  • 软件项目规模估计方法介绍
  • vim编辑器使用[学习札记]
  • JAVA简单吗?
  • 并发问题及控制手段
  • 我对关Java乱码产生与解决的认识
  • 宽带无线技术网络部署
  • Unigraphics NX 2.0.6.2 Update
  • 对***Lee控件集合(***Lee.Framework)的相似性分析
  • Illustrator 黑白箭头完全剖析(2)
  • GNU交叉工具链(arm-linux-gcc 3.4.4)
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.