« Torna a Build Services...

Connect to Another Datasource (Legacy)

(Redirezionato da Connecting to Another DatasourceDatabase)

This is based off the Library portlet example and the 4.1.2 tomcat bundle. A section has been added with the specific changes needed for 4.2.x and 4.3.x.

ROOT.xml #

Add another datasource

 	<Resource
 			name="jdbc/LiferayPool"
 			auth="Container"
 			type="javax.sql.DataSource"
 			driverClassName="com.mysql.jdbc.Driver"
 			url="jdbc:mysql://localhost/lportal?useUnicode=true&amp;characterEncoding=UTF-8"
 			username="root"
 			password="asdf"
 			maxActive="20"
 	/>
 	<Resource
 			name="jdbc/TrainingPool"
 			auth="Container"
 			type="javax.sql.DataSource"
 			driverClassName="com.mysql.jdbc.Driver"
 			url="jdbc:mysql://localhost/training?useUnicode=true&amp;characterEncoding=UTF-8"
 			username="root"
 			password="asdf"
 			maxActive="20"
 	/>

ext-spring-training.xml #

Create a file that will override the default values of ext-spring-professional.xml. ( Example ext-spring-training.xml )

 	<bean id="trainingDataSourceTarget" class="com.liferay.portal.spring.jndi.JndiObjectFactoryBean" lazy-init="true">
 		<property name="jndiName">
 			<value>jdbc/TrainingPool</value>
 		</property>
 	</bean>
 	<bean id="trainingDataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy" lazy-init="true">
 		<property name="targetDataSource">
 			<ref bean="trainingDataSourceTarget" />
 		</property>
 	</bean>
 	<bean id="trainingSessionFactory" class="com.liferay.portal.spring.hibernate.HibernateConfiguration" lazy-init="true">
 		<property name="dataSource">
 			<ref local="trainingDataSource" />
 		</property>
 	</bean>
 	<bean id="trainingTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" lazy-init="true">
 		<property name="dataSource">
 			<ref bean="trainingDataSource" />
 		</property>
 		<property name="sessionFactory">
 			<ref bean="trainingSessionFactory" />
 		</property>
 	</bean>

Replace the liferay ones with the training references.

 	<bean id="com.ext.portlet.library.service.spring.BookLocalService.professional" class="com.ext.portlet.library.service.impl.BookLocalServiceImpl" lazy-init="true" />
 	<bean id="com.ext.portlet.library.service.spring.BookLocalService.transaction" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true">
 		<property name="transactionManager">
 			<ref bean="trainingTransactionManager" />
 		</property>
 		<property name="target">
 			<ref bean="com.ext.portlet.library.service.spring.BookLocalService.professional" />
 		</property>
 		<property name="transactionAttributes">
 			<props>
 				<prop key="*">PROPAGATION_REQUIRED</prop>
 			</props>
 		</property>
 	</bean>
 	<bean id="com.ext.portlet.library.service.spring.BookLocalServiceFactory" class="com.ext.portlet.library.service.spring.BookLocalServiceFactory" lazy-init="true">
 		<property name="service">
 			<ref bean="com.ext.portlet.library.service.spring.BookLocalService.transaction" />
 		</property>
 	</bean>
 	<bean id="com.ext.portlet.library.service.spring.BookService.professional" class="com.ext.portlet.library.service.impl.BookServiceImpl" lazy-init="true" />
 	<bean id="com.ext.portlet.library.service.spring.BookService.transaction" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true">
 		<property name="transactionManager">
 			<ref bean="trainingTransactionManager" />
 		</property>
 		<property name="target">
 			<ref bean="com.ext.portlet.library.service.spring.BookService.professional" />
 		</property>
 		<property name="transactionAttributes">
 			<props>
 				<prop key="*">PROPAGATION_REQUIRED</prop>
 			</props>
 		</property>
 	</bean>
 	<bean id="com.ext.portlet.library.service.spring.BookServiceFactory" class="com.ext.portlet.library.service.spring.BookServiceFactory" lazy-init="true">
 		<property name="service">
 			<ref bean="com.ext.portlet.library.service.spring.BookService.transaction" />
 		</property>
 	</bean>
 	<bean id="com.ext.portlet.library.service.persistence.BookPersistence" class="com.ext.portlet.library.service.persistence.BookPersistence" lazy-init="true">
 		<property name="dataSource">
 			<ref bean="trainingDataSource" />
 		</property>
 		<property name="sessionFactory">
 			<ref bean="trainingSessionFactory" />
 		</property>
 	</bean>
 	<bean id="com.ext.portlet.library.service.persistence.BookUtil" class="com.ext.portlet.library.service.persistence.BookUtil" lazy-init="true">
 		<property name="persistence">
 			<ref bean="com.ext.portlet.library.service.persistence.BookPersistence" />
 		</property>
 	</bean>

service.xml #

For 4.2.X:

 <entity name="Book" local-service="true" remote-service="false" data-source="trainingDataSource" session-factory="trainingSessionFactory">

For 4.3.X

 <entity name="Book" local-service="true" remote-service="false" data-source="trainingDataSource" session-factory="trainingSessionFactory" tx-manager="trainingTransactionManager">

portal-ext.properties #

Add your new xml to the list at the end of the spring.configs property.

 spring.configs=META-INF/counter-spring.xml,...,META-INF/portal-spring-jcr.xml,META-INF/portal-spring-jms.xml,META-INF/ext-spring.xml,META-INF/ext-spring-training.xml

Troubleshooting #

  • Make sure you do a "ant clean deploy" after you make the changes.
  • For war datasource/database access, please refer to our sample DAO portlet's code.
0 Allegati
19907 Visualizzazioni
Media (0 Voti)
La media del punteggio è 0.0 stelle su 5.
Commenti