Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Ohad Raz
Service Builder With Custom DataSource Breaks Hot Deploy
January 17, 2013 3:43 AM
Answer

Ohad Raz

Rank: New Member

Posts: 23

Join Date: June 27, 2012

Recent Posts

Hi,

I have created a simple service builder that uses a custom data source.
As a result, the hot-deploy functionality got broken.
When trying to hot-deploy the following exception appears in the server's log:

com.liferay.portal.kernel.exception.SystemException: com.liferay.portal.kernel.dao.orm.ORMException: java.lang.IllegalStateException: The com.liferay.portal.model.impl.CompanyImpl Cache is not alive.
com.liferay.portal.kernel.exception.SystemException: com.liferay.portal.kernel.dao.orm.ORMException: java.lang.IllegalStateException: The com.liferay.portal.model.impl.CompanyImpl Cache is not alive.
at com.liferay.portal.service.persistence.impl.BasePersistenceImpl.processException(BasePersistenceImpl.java:190)
at com.liferay.portal.service.persistence.CompanyPersistenceImpl.fetchByPrimaryKey(CompanyPersistenceImpl.java:488)
at com.liferay.portal.service.persistence.CompanyPersistenceImpl.findByPrimaryKey(CompanyPersistenceImpl.java:432)
at com.liferay.portal.service.impl.CompanyLocalServiceImpl.getCompanyById(CompanyLocalServiceImpl.java:461)
at sun.reflect.GeneratedMethodAccessor219.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
...

(The same exception repeats for all kind of Liferay's objects, not necessary the Company alone, e.g., Layout).

I have built my service builder based on the tips in the following blog:
http://www.liferay.com/about-us/privacy/-/blogs/6831821/maximized
It all seemed to work, but when I tried to hot-deploy, the aforementioned exceptions were observed.

My service.xml looked as follows:
 1<?xml version="1.0" encoding="UTF-8"?>
 2<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.1.0//EN"
 3          "http://www.liferay.com/dtd/liferay-service-builder_6_1_0.dtd">
 4
 5<service-builder package-path="com.company.project.entity" auto-namespace-tables="true">
 6   <author><![CDATA[<a href="ohad.raz@orbitz.com">Raz, Ohad</a>]]></author>
 7
 8   <namespace>entity</namespace>
 9   <entity name="Entity"
10           local-service="true"
11           human-name="Entities"
12           table="TB_ENTITY"
13           data-source="company-project-DS"
14           session-factory="company-project-SessionFactory"
15           tx-manager="company-project-TransactionManager">
16
17      <!-- PK fields -->
18      <column db-name="TB_ENTITY_ID" name="entityID" primary="true" type="long" />
19
20      <!-- Audit fields -->
21      <column db-name="CREATE_DATE" name="createDate" type="Date" />
22      <column db-name="MODIFIED_DATE" name="modifiedDate" type="Date" />
23
24      <!-- Other fields -->
25      <column db-name="COLUMN_A" name="columnA" type="long" />
26      <column db-name="COLUMN_B" name="columnB" type="String" />
27      <column db-name="COLUMN_C" name="columnC" type="Date" />
28
29      <!-- Finder methods -->
30      <finder name="ColumnA" return-type="Entity">
31         <finder-column name="columnA" />
32      </finder>
33   </entity>
34</service-builder>

And my ext-spring.xml looked as follows:
 1<?xml version="1.0" encoding="UTF-8"?>
 2
 3<beans xmlns="http://www.springframework.org/schema/beans"
 4        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 6
 7   <bean id="company-project-SessionFactory" class="com.liferay.portal.dao.orm.hibernate.SessionFactoryImpl" lazy-init="true">
 8      <property name="sessionFactoryImplementor">
 9         <bean class="com.liferay.portal.spring.hibernate.PortalHibernateConfiguration" lazy-init="true">
10            <property name="dataSource">
11               <ref bean="company-project-DS" />
12            </property>
13            <property name="mappingResources">
14               <list>
15                  <value>META-INF/portlet-hbm.xml</value>
16               </list>
17            </property>
18         </bean>
19      </property>
20   </bean>
21
22   <bean id="company-project-DS" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy" lazy-init="true">
23      <property name="targetDataSource">
24         <bean class="com.liferay.portal.spring.jndi.JndiObjectFactoryBean" lazy-init="true">
25            <property name="jndiName">
26               <value>jdbc/CompanyProjectData</value>
27            </property>
28         </bean>
29      </property>
30   </bean>
31
32   <bean id="company-projec-TransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" lazy-init="true">
33      <property name="dataSource">
34         <ref bean="company-projec-DS" />
35      </property>
36      <property name="sessionFactory">
37         <ref bean="company-projec-SessionFactory" />
38      </property>
39   </bean>
40</beans>


So, where was the problem?
Ohad Raz
RE: Service Builder With Custom DataSource Breaks Hot Deploy
January 17, 2013 4:23 AM
Answer

Ohad Raz

Rank: New Member

Posts: 23

Join Date: June 27, 2012

Recent Posts

So, I have came across the following thread:
http://www.liferay.com/community/forums/-/message_boards/message/13076583
which, in its turn, points to http://www.liferay.com/community/forums/-/message_boards/message/10713975.
So I realized that the problem is that I have used com.liferay.portal.spring.hibernate.PortalHibernateConfiguration as the Session Factory Implementor.
I looked at the source code of this class, and indeed it seems to be messing Liferay's internals, and should be used for custom data sources and entities.
So, I tried the suggestions in the aforementioned thread, using org.springframework.orm.hibernate3.LocalSessionFactoryBean as the Session Factory Implementor.
But then I kept getting org.hibernate.HibernateException: Unable to get the default Bean Validation factory exception.
I googled it, and found that I had to add a hibernate-validator.jar to my classpath.
I have tried several jars, of different versions, but nothing helped.
I finally found the properties needed to disable the validator, but then I got exceptions about cache - either enabling, it, or missing provider...
Another google, and...
Eureka!
It all works! Liferay starts up with no exceptions, it runs fine, and I can hot-deploy!!!

What's changed?

Well, this is the ext-spring.xml that made it all to work:
 1<?xml version="1.0" encoding="UTF-8"?>
 2<beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 5
 6   <bean id="company-project-SessionFactory" class="com.liferay.portal.dao.orm.hibernate.SessionFactoryImpl" lazy-init="true">
 7      <property name="sessionFactoryImplementor">
 8         <bean class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" lazy-init="true">
 9            <property name="configurationClass">
10               <value>org.hibernate.cfg.Configuration</value>
11            </property>
12            <property name="dataSource">
13               <ref bean="company-project-DS" />
14            </property>
15            <property name="mappingResources">
16               <list>
17                  <value>META-INF/portlet-hbm.xml</value>
18               </list>
19            </property>
20            <property name="hibernateProperties">
21               <props>
22                  <prop key="hibernate.validator.apply_to_ddl">false</prop>
23                  <prop key="hibernate.validator.autoregister_listeners">false</prop>
24                  <prop key="javax.persistence.validation.mode">none</prop>
25                  <prop key="hibernate.cache.use_second_level_cache">true</prop>
26                  <prop key="hibernate.cache.use_query_cache">true</prop>
27                  <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
28               </props>
29            </property>
30         </bean>
31      </property>
32   </bean>
33
34   <bean id="company-project-DS" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy" lazy-init="true">
35      <property name="targetDataSource">
36         <bean class="com.liferay.portal.spring.jndi.JndiObjectFactoryBean" lazy-init="true">
37            <property name="jndiName">
38               <value>jdbc/CompanyProjectData</value>
39            </property>
40         </bean>
41      </property>
42   </bean>
43
44   <bean id="company-project-TransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" lazy-init="true">
45      <property name="dataSource">
46         <ref bean="company-project-DS" />
47      </property>
48      <property name="sessionFactory">
49         <ref bean="company-project-SessionFactory" />
50      </property>
51   </bean>
52</beans>


Hope it can help anyone encountering the same problems...

Cheers,
Ohad
David H Nebinger
RE: Service Builder With Custom DataSource Breaks Hot Deploy
January 17, 2013 6:07 AM
Answer

David H Nebinger

Community Moderator

Rank: Liferay Legend

Posts: 8424

Join Date: September 1, 2006

Recent Posts

Not sure what you've used, but here's how I define external db connections...

 1
 2 <bean id="myDataSource" lazy-init="true" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
 3    ....
 4 </bean>
 5 <bean id="myHibernateSessionFactory" class="com.liferay.portal.spring.hibernate.PortletHibernateConfiguration">
 6    <property name="dataSource" ref="myDataSource" />
 7    ....
 8 </bean>
 9 <bean id="mySessionFactory" class="com.liferay.portal.dao.orm.hibernate.PortletSessionFactoryImpl">
10    <property name="dataSource" ref="myDataSource" />
11    <property name="sessionFactoryClassLoader" ref="portletClassLoader" />
12    <property name="sessionFactoryImplementor" ref="myHibernateSessionFactory" />
13 </bean>


This way I'm still using all Liferay entities (don't know if it's necessary, but maximizes compatibility).

Also I don't need to define anything for transaction handling (the default Liferay transaction handling has been good enough).

As far as hibernate validator was concerned, I too had to add it, but version 4.3.0 worked just fine. Needed the hibernate-validator-4.3.0.Final.jar and hibernate-validator-annotation-processor-4.3.0.Final.jar in my lib folder, but had no version issues to deal with.

And I didn't have to disable the cache!
Konstantin Chudinov
RE: Service Builder With Custom DataSource Breaks Hot Deploy
May 2, 2013 11:11 AM
Answer

Konstantin Chudinov

Rank: Junior Member

Posts: 43

Join Date: April 23, 2013

Recent Posts

I have the same issue! But in my case, everything is not so simpleemoticon
I also used this awrful blog:
http://www.liferay.com/about-us/privacy/-/blogs/6831821/maximized
It's stale!
Don't use it!
In my case liferay don't see
<property name="mappingResources">
<list>
<value>META-INF/portlet-hbm.xml</value>
</list>
</property>

I deleted these strings and I cought error with hibernate sql. i dont inderstand the reason, and I don't want to! I have small time to solve this problem and I spent the whole day for it.

DO SOMEBODY HAS COMPLETE WORKING EXAMPLE FOR LIFERAY 6.1 for connecting to external DB???
Francis Franco Freich
RE: Service Builder With Custom DataSource Breaks Hot Deploy
May 3, 2013 12:58 AM
Answer

Francis Franco Freich

Rank: New Member

Posts: 10

Join Date: May 16, 2011

Recent Posts

Konstantin Chudinov:
I have the same issue! But in my case, everything is not so simpleemoticon
I also used this awrful blog:
http://www.liferay.com/about-us/privacy/-/blogs/6831821/maximized
It's stale!
Don't use it!
In my case liferay don't see
<property name="mappingResources">
<list>
<value>META-INF/portlet-hbm.xml</value>
</list>
</property>

I deleted these strings and I cought error with hibernate sql. i dont inderstand the reason, and I don't want to! I have small time to solve this problem and I spent the whole day for it.

DO SOMEBODY HAS COMPLETE WORKING EXAMPLE FOR LIFERAY 6.1 for connecting to external DB???


Are you tied to particular technologies? Other than Liferay of course.
Harry Mark
RE: Service Builder With Custom DataSource Breaks Hot Deploy
May 5, 2013 5:14 PM
Answer

Harry Mark

Rank: New Member

Posts: 1

Join Date: October 6, 2010

Recent Posts

To get it working I searched hibernate-spring.xml (in Liferay source) for bean definitions that reference "liferayDataSource". Copied the definitions and renamed them, changing some of the properties for the portlet. Note: jdbc/ExternalPool is a global Tomcat data source.

Here's the ext-spring.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="externalDataSource" class="com.liferay.portal.spring.jndi.JndiObjectFactoryBean" lazy-init="true">
<property name="jndiName">
<value>jdbc/ExternalPool</value>
</property>
</bean>

<bean id="externalHibernateSessionFactory" class="com.liferay.portal.spring.hibernate.PortalHibernateConfiguration">
<property name="dataSource" ref="externalDataSource" />
<property name="mappingResources">
<list>
<value>/META-INF/portlet-hbm.xml</value>
</list>
</property>
</bean>

<bean id="externalSessionFactory" class="com.liferay.portal.dao.orm.hibernate.SessionFactoryImpl">
<property name="sessionFactoryClassLoader" ref="portletClassLoader" />
<property name="sessionFactoryImplementor" ref="externalHibernateSessionFactory" />
</bean>

<bean id="externalTransactionManager" class="com.liferay.portal.spring.transaction.TransactionManagerFactory" factory-method="createTransactionManager">
<constructor-arg ref="externalDataSource" />
<constructor-arg ref="externalHibernateSessionFactory" />
</bean>
</beans>


To use the external data source in service.xml objects, you need to set the data-source, session-factory & tx-manager attributes to these bean IDs. Run the "build-service" target then "deploy":

<entity name="Student" local-service="true" remote-service="false" data-source="externalDataSource" session-factory="externalSessionFactory" tx-manager="externalTransactionManager">
....


Note that the corresponding database objects must already exist in the external DB.