Connect to second database in Liferay(连接第二个数据库)

同时连接两个数据库,连接多个数据库

这是我自己研究出来的,真的可以说是世界首发了,网上也有一些例子,都要么就是老版本,要么就特复杂。我的这个方法特简单,希望对大家有用,也希望大家珍惜

因为公司的需要,直接写的英文版的,不想再翻译回来了。都不难理解,一看就能明白。不懂的请留言

Introduction

                First of all, I should say that setting liferay to connect to another database is very easy. Certainty, all of things to make easy always after numerous attempts . I searched several solutions from website before. But, not only for old version but also too complex.  As my understand  According to original source, and several trial. I found the way. And I think that it is the World Premiere.^_^

 

Prerequisites

You should know how to create services with Service Builder, Also You should know how to create new portlet. how to link to database and  how it is used inside programming language code.

The steps described here were performed over Liferay 6.0.6.

 

 

Let us Beginning

1.       Create a file named portal-ext.properties in {WEBSERVER-HOME}\webapps\ROOT\WEB-INF\classes. And type the following code:

    #
    # MySQL
    #
    jdbc.default.driverClassName
=com.mysql.jdbc.Driver
    jdbc.default.url
=jdbc:mysql://localhost/lfportal?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
    jdbc.default.username
=root
    jdbc.default.password
=root  
  
    #
    # This is the second connection
    #
    jdbc.second.driverClassName
=net.sourceforge.jtds.jdbc.Driver
    jdbc.second.url
=jdbc:jtds:sqlserver://localhost:1433/extraDB
    jdbc.second.username
=sa
    jdbc.second.password
=test

 

2.       Create a service.xml file. And type the following code:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.0.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_0_0.dtd">
<service-builder package-path="com.test">
    
<author>noah.xiang</author>
    
<namespace>nx</namespace>

    
<entity name="MyTable" local-service="true" remote-service="false" table="myTable" data-source="secondDataSource" session-factory="secondSessionFactory" tx-manager="secondTransactionManager">
        
<!-- PK fields -->

        
<column name="id" type="int" primary="true" />

        
<!-- Audit fields -->

        
<column name="jahr" type="Date" mapping-key="[Jahr]"/>
        
<column name="beschaffungsstrategie" type="String"  mapping-key="[Beschaffungsstrategie]"/>
        
<column name="buchVon" type="String" mapping-key="[Buch von]"/>
        
    
</entity>
</service-builder>

 

3.       Ant build-service.xml, let liferay to generate code.

4.       Create a new file named ext-spring.xml in src\META-INF folder. And copy the following code:

 

<?xml version="1.0"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    
<bean id="secondDataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy" lazy-init="true">
        
<property name="targetDataSource">
            
<bean class="com.liferay.portal.dao.jdbc.util.DataSourceFactoryBean">
                
<property name="propertyPrefix" value="jdbc.second." />
            
</bean>
        
</property>
    
</bean>
    
<bean id="secondHibernateSessionFactory" class="com.liferay.portal.spring.hibernate.PortalHibernateConfiguration" lazy-init="true">
        
<property name="dataSource">
            
<ref bean="secondDataSource" />
        
</property>
        
<property name="mappingResources">
            
<list>
                
<value>META-INF/portlet-hbm.xml</value>
            
</list>
        
</property>
    
</bean>
    
<bean id="secondSessionFactory" class="com.liferay.portal.dao.orm.hibernate.SessionFactoryImpl" lazy-init="true">
        
<property name="sessionFactoryImplementor">
            
<ref bean="secondHibernateSessionFactory" />
        
</property>
    
</bean>
    
<bean id="secondTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" lazy-init="true">
        
<property name="dataSource">
            
<ref bean="secondDataSource" />
        
</property>
        
<property name="sessionFactory">
            
<ref bean="secondHibernateSessionFactory" />
        
</property>
    
</bean>
</beans>

 

5.       No more. So easy right? About how to call and deploy it. I believe that you can do.

 



眼镜蛇

posted on 2011-10-27 17:56 眼镜蛇 阅读(1407) 评论(6)  编辑  收藏 所属分类: Liferay

评论

# re: Connect to second database in Liferay(连接第二个数据库) 2012-03-01 14:31 彭维

234148464 qq求助!实在是运行不通。。。  回复  更多评论   

# re: Connect to second database in Liferay(连接第二个数据库) 2012-03-02 12:24 Noah

哪里不通  回复  更多评论   

# re: Connect to second database in Liferay(连接第二个数据库) 2012-03-05 10:45 彭维

研究了一天,完全让你的做的,不过板本是6.1,行不通,求大神赐教。。。I need your help,service builder的时候不能够生成表,跟本连不到另一个数据源上面去。。  回复  更多评论   

# re: Connect to second database in Liferay(连接第二个数据库) 2012-03-05 10:51 彭维

I am so sad! for a long time ,just bcz of this porblem.....,  回复  更多评论   

# re: Connect to second database in Liferay(连接第二个数据库) 2012-03-20 21:50 Noah

@彭维
如果是6.1的话,需要修改 com.liferay.portal.spring.hibernate.PortalHibernateConfiguration

com.liferay.portal.spring.hibernate.PortletHibernateConfiguration  回复  更多评论   

# re: Connect to second database in Liferay(连接第二个数据库)[未登录] 2013-01-30 18:08 Owen

跨兩個db要怎樣做transaction

按照你這樣的寫法,如果拋出exception,只會有其中一個DB ROLLBACK,另一個db不會rollback,有什麼好解法嗎?

jta or jotm,這兩個還要研究看看怎樣用在liferay上面  回复  更多评论   


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


网站导航:
 
<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

常用链接

留言簿(6)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜