Liferay同时连接多个数据库及其事务问题
首先我们来看他的liferay-service-builder_4_2_0.dtd,在Element column中增加了两项属性data-source CDATA #IMPLIED和 session-factory CDATA #IMPLIED,对于这两个属性文档中是这样描述的:
- The data-source value specifies the the data source target that is set to the
- persistence class. The default value is the Liferay data source. This is used in
- conjunction with session-factory.
- The session-factory value specifies the the session factory that is set to the
- persistence class. The default value is the Liferay session factory. This is
- used in conjunction with data-source
指定了多个不同的数据库,取得了不同的connection,那从不同的sessionfactory得到了不同的transaction instance,大家担心的就是分段式事务问题,而liferay的事务处理是由spring support的,我们可以先看看spring中的PlatformTransactionManager
- public interface PlatformTransactionManager {
- TransactionStatus getTransaction(TransactionDefinition definition)
- throws TransactionException;
- void commit(TransactionStatus status) throws TransactionException;
- void rollback(TransactionStatus status) throws TransactionException;
- }
当程序由于事务问题抛出异常的时候,spring文档是这样描述的:
Again in keeping with Spring's philosophy, the TransactionException that can be thrown by any of the
PlatformTransactionManager interface's methods is unchecked (i.e. it extends the
java.lang.RuntimeException class). Transaction infrastructure failures are almost invariably fatal. In rare
cases where application code can actually recover from a transaction failure, the application developer can still
choose to catch and handle TransactionException. The salient point is that developers are not forced to do so.
看spring的源码,你会发现当程序在运行过程中抛出unchecked exception的时候,transaction会设为rollback only的status从而回滚事务。所以我们设想只要抛出unchecked exception的时候,事务很同时回滚。通过测试,事务不能同时回滚。主要原因是因为处于不同的sessionfactory中,就是说两个事务之间没有任何的联系。
分布式事务,ejb方有个很好的解决方案,至于在liferay中如何运用,还需时间去研究。
在liferay这样的活动性高的开源平台下做开发,未免升级时候会遇到种种问题,其中一个就是数据库的升级问题,所以一个很好的方案是把liferay的数据库和业务数据库分开。虽然事务没有按预期测试成功,但是对于多个没有事务关联的数据库来说,这个是个很好的解决方案,而且连接不同数据库是通过配置完成,对开发人员是透明的。希望这篇文章能对运用liferay的朋友有帮助。同时,欢迎大家针对连接多个数据库的方案特别是事务问题作出讨论。
- 12:46
- 浏览 (4271)
- 论坛浏览 (4669)
- 评论 (6)
- 分类: liferay
- 发布在 liferay 圈子
- 相关推荐
评论
不错,但是没有说到点上。具体怎么做呢?一个很好的方案是把liferay的数据库和业务数据库分开
我觉得有些问题不用描述得太详细,因为我给出的wiki(http://wiki.liferay.com/index.php/Connecting_to_Another_Datasource/Database)链接中已经有很详细的说明应该具体怎么做,我希望大家能从一些指导性的文章中学会看官方的文档来解决问题,而不是像小孩学说话一样。
而这篇文章的目的,可以说要点是阐述一个系统数据和业务数据分开的观点,另外一个要点就是想讨论如何解决事务问题,我希望大家能针对如何解决这个事务问题展开讨论
<bean id="lingeasyDataSourceTarget" class="com.liferay.portal.spring.jndi.JndiObjectFactoryBean" lazy-init="true">
<property name="jndiName">
<value>jdbc/ErpBoPool</value>
</property>
</bean>
<bean id="ErpBoDataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy" lazy-init="true">
<property name="targetDataSource">
<ref bean="ErpBoDataSourceTarget" />
</property>
</bean>
<bean id="ErpBoSessionFactory" class="com.liferay.portal.spring.hibernate.HibernateConfiguration" lazy-init="true">
<property name="dataSource">
<ref bean="ErpBoDataSource" />
</property>
</bean>
接下去就不用多说了吧。早在以前的版本就可以实现的。如果大家有更好的办法请赐教!
发表评论
该博客是同时发布到论坛的,无法评论在论坛已被锁定的帖子
我的相册
共 12 张
最新评论
-
liferay权限开发(一)
我想知道程序 验证 的过程。(代码级别) 可否告知? 续集在那里?
-- by java.fan -
提高Liferay生产环境下的 ...
没有,哪里的gmail?
-- by devilbaby -
提高Liferay生产环境下的 ...
devilbaby你好,以前你联系过 liferaychina@gmail.co ...
-- by 010lead -
利用Liferay开发portal应 ...
hhhhhh
-- by yangzh -
利用Liferay开发portal应 ...
我现在遇到了一个问题,再问,问题之前。我想先确认一下我做的是否正确。 是不是在D ...
-- by 眼镜蛇







评论排行榜