昨天还在用Torque,今天听说Hibernate更好,明天说不准又有更好的持久层工具...

如何使应用程序不变化的情况下选择不同的持久层呢?

看看Finetix LLC的资深顾问Charles Chan给我们现身说法。

Charles Chan在文章 Object-Relational Mapping with Apache Jakarta OJB

中介绍了Apache OJB的使用,并在文章附录中提到一种屏蔽不同持久层的设计思路,他采用了工厂方法模式(Factory Method),并通过实例Apache OJB来说明他的设计。

这个设计有两个产品,一个是PersistenceManager,另一个是Transaction 。

OJBPersistenceManager和OJBTransaction 分别是两个产品的实现。

It is often desirable to decouple your application from the specifics of a persistence framework, so that you can easily switch frameworks in the future. I recommend creating custom PersistenceManager and Transaction classes for this purpose. For example:

Example

13

: PersistenceManager and Transaction Interfaces

/** */

/**

* The abstract base class for all PersistenceManager. It provides

* method to persist and object and to obtain the current

* transaction.

*/

public

abstract

class

PersistenceManager

{

public

static

PersistenceManager getInstance()

{

return

new

OJBPersistenceManager();

}

public

abstract

void

makePersistent(Object o)

throws

PersistenceException;

public

abstract

Transaction currentTransaction()

throws

PersistenceException;

}

/** */

/**

* A Transaction interface. It provides minimal support for

* beginning, committing, and rolling back a transaction.

*/

public

interface

Transaction

{

public

void

begin()

throws

PersistenceException;

public

void

commit()

throws

PersistenceException;

public

void

rollback()

throws

PersistenceException;

}

/** */

/**

* An OJB specific implementation of the PersistenceManager

* class.

*/

public

class

OJBPersistenceManager

extends

PersistenceManager

{

private

PersistenceBroker broker

=

null

;

public

OJBPersistenceManager()

{

broker

=

PersistenceBrokerFactory.defaultPersistenceBroker();

}

public

void

makePersistent(Object o)

throws

PersistenceException

{

try

{

broker.store(o);

}

catch

(PersistenceBrokerException e)

{

//

rethrow PersistenceException

}

}

public

Transaction currentTransaction()

throws

PersistenceException

{

return

new

OJBTransaction(broker);

}

}

/** */

/**

* An OJB specific implementation of the Transaction

* interface.

*/

public

class

OJBTransaction

implements

Transaction

{

private

PersistenceBroker broker

=

null

;

public

OJBTransaction(PersistenceBroker broker)

{

this

.broker

=

broker;

}

public

void

begin()

throws

PersistenceException

{

try

{

broker.beginTransaction();

}

catch

(TransactionAbortedException e)

{

//

rethrow PersistenceException

}

catch

(TransactionInProgressException e)

{

//

rethrow PersistenceException

}

}

public

void

commit()

throws

PersistenceException

{

try

{

broker.commitTransaction();

}

catch

(TransactionAbortedException e)

{

//

rethrow PersistenceException

}

catch

(TransactionNotInProgressException e)

{

//

rethrow PersistenceException

}

}

public

void

rollback()

throws

PersistenceException

{

try

{

broker.abortTransaction();

}

catch

(TransactionNotInProgressException e)

{

//

rethrow PersistenceException

}

}

}

After the above classes are created, it is very simple to convert the test application to use them:

下面是这个Factory的使用:

Example

14

: Example Using PersistenceManager and Transaction Interfaces

PersistenceManager pm

=

PersistenceManager.getInstance();

Transaction tx

=

pm.currentTransaction();

tx.begin();

Manager manager

=

newManager();

manager.setName(

"

John Smith

"

);

manager.setProjectNumber(

new

Integer(

10

));

pm.makePersistent(manager);

Employee employee

=

newEmployee();

employee.setName(

"

David Cosby

"

);

pm.makePersistent(employee);

tx.commit();

It looks surprisingly like JDO, doesn't it? With a little more effort, you can also hide the OJB query mechanism.

改进建议:

1. 改用抽象工厂方法, 一套持久层产品提供一个工厂,用于创建不同的产品(如:PersistenceManager,Transaction....)

2.考虑从不同的持久层产品中抽取一套通用的持久层接口

persistence java_Java数据持久层 - Persistence Manager Factory相关推荐

  1. 后端常用数据持久层模板及框架以及一些工具类模板的配置使用集合

    文章目录 后端常用数据持久层模板及框架以及一些工具类模板的配置使用集合 JDBC.c3p0.hibernate配置模板:JDBC模板方法模式.抽象工厂模式封装模板:Spring+hibernate+c ...

  2. Web开发的那点事--数据持久层常用功能

    CSDN博客不再经常更新,更多优质文章请来 粉丝联盟网 FansUnion.cn! (FansUnion) 数据持久层--4类功能 1.增加  增加一条记录-实体(有关联,没有关联)   2.修改   ...

  3. 轻量级数据持久层Seaking.PL简介及建议征集

    引言 Seaking.PL(以下简称PL)是我刚刚完成的一个轻量级数据持久层,在众多的数据持久层产品中,PL或许显得有些稚嫩,甚至有点难登大雅之堂,但是我相信,至于对于一部分应用,PL可能会是一个简易 ...

  4. 持久层是什么意思_软件项目实训及课程设计指导—如何在数据持久层中应用DAO模式...

    软件项目实训及课程设计指导--如何在J2EE应用系统数据持久层中应用DAO模式 1.为什么要在软件应用系统中提供数据持久层 软件应用系统中的数据持久层主要为整个软件应用系统提供数据访问功能服务,从而可 ...

  5. Java数据持久层框架 MyBatis之背景知识一

    对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...

  6. 数据持久层框架 Hibernate

    Hibernate是一种Java语言下的对象关系映射解决方案. 它是使用GNU宽通用公共许可证发行的自由.开源的软件.它为面向对象的领域模型到传统的关系型数据库的映射,提供了一个使用方便的框架.Hib ...

  7. MyBatis 一个数据持久层(ORM)框架

    MyBatis 的前身就是 iBatis .是一个数据持久层(ORM)框架. MyBatis框架 [1] iBATIS一词来源于"internet"和"abatis&qu ...

  8. MyBatis 数据持久层

    引用:http://baike.baidu.com/view/4372646.htm MyBatis 的前身就是 iBatis .是一个数据持久层(ORM)框架. MyBatis框架 [1] iBAT ...

  9. .NET开源项目介绍及资源推荐:数据持久层

    在.NET平台下,关于数据持久层框架非常多,本文主要对如下几种做简要的介绍并推荐一些学习的资源: 1.NHibernate 2.NBear 3.Castle ActiveRecord 4.iBATIS ...

最新文章

  1. Java基础篇:多线程
  2. Android中设置EditText默认无焦点
  3. linux cache buffer区别,Linux buffer/cache异同
  4. shell读取文件并且遍历输出
  5. Windows Server 2012 R2 里面如何安装Net Framework 3.5
  6. SAP License:老板都不懂ERP,怎么会用得好ERP呢?
  7. metamask插件_Remix+metamask+myetherwallet+Ciper 完成主网发币(实战 十)
  8. 【刘文彬】 Controller:EOS区块链核心控制器
  9. python-知识回顾-16
  10. 确定数组是否包含值[重复]
  11. 时序分析基本概念介绍<input/output delay>
  12. 针对form表单赋值封装
  13. Mongodb Manual阅读笔记:CH2 Mongodb CRUD 操作
  14. Mybatis 常用开发注解(批量插入)
  15. opencv前景检测
  16. mPaSS小程序 路由跳转
  17. 虚拟声卡实现播放铃声
  18. PHP开源管理系统 Gms,GMS 一个成熟的gms系统,拥有基本的权限管理和分类,后台很好 Windows Develop 249万源代码下载- www.pudn.com...
  19. 傲骨贤妻第一季/全集The Good Wife迅雷下载
  20. Python+matplotlib绘制地图

热门文章

  1. 在centos7中安装nodejs(npm )
  2. 转:CentOS系统yum源配置修改、yum安装软件包源码包出错解决办法!
  3. 《C++并发编程实战》——1.1 什么是并发
  4. I.MX6 Android 设备节点权限
  5. rhcs实现mysql高可用。仲裁磁盘
  6. Python基础(2):控制结构
  7. 不能不说的C#特性-迭代器(下),yield以及流的延迟计算
  8. braft的LogEntry日志记录存储LogStorage
  9. 8-9 实现原理-1
  10. 不抛出异常的swap