Liferay sessionFactory.getCurrentSession()报错问题

来源:互联网 发布:淘宝都是支付宝登录吗 编辑:程序博客网 时间:2024/06/11 06:34

刚开始研究liferay不久,不过对liferay的功能和用的技术真多比较佩服.不过对于开发人员来说要深入了解那么的技术的不是那么容易的事情.

liferay的资料相对较少,不过去官方的论坛和wiki里面找一些东西.

 

最近在使用liferay的plugin portlet开发方式.

最简单的功能就是直接从数据库查询数据,插入数据等.

插入数据的话用liferay的build service工具就能自动提供接口不用那么麻烦.

 

不过查询的时候从BasePersistenceImpl类的openSession()的时候报了下面的错误.

com.liferay.portal.kernel.exception.SystemException: org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
    at com.test.finder.service.persistence.TestEntryFinderImpl.findByNoAssets(TestEntryFinderImpl.java:256)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:80)
    at com.liferay.portal.dao.shard.ShardAdvice.invokePersistence(ShardAdvice.java:205)
    at sun.reflect.GeneratedMethodAccessor524.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610)
    at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:65)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    at $Proxy348.findByNoAssets(Unknown Source)

 

 

郁闷了好几天.找了好些liferay的东西看也不知道解决办法.

最近跟踪代码,发现里面是sessionFactory.getCurrentSession()这个方法报错.

 

当时想你报错那我就不用你好了,用Session openNewSession(Connection connection)这个方法好了.

openNewSession这个方法倒是能够正常访问了,但这个是每次都打开一个新连接,然后关闭.

还是感觉用springde东西比较好.

没办法回到sessionFactory.getCurrentSession()为什么出错上来,在网上找了很久,才发现了解决办法.

service builder执行后的默认定义.

   <bean id="liferayHibernateSessionFactory"
        class="com.liferay.portal.spring.hibernate.PortletHibernateConfiguration">
        <property name="dataSource" ref="liferayDataSource" />
    </bean>
   
   
修改后的定义:
    <bean id="liferayHibernateSessionFactory"
        class="com.liferay.portal.spring.hibernate.PortletHibernateConfiguration">
        <property name="dataSource" ref="liferayDataSource" />
         <property name="hibernateProperties">
            <props>
                <prop key="hibernate.current_session_context_class">thread</prop>
            </props>
        </property>

    </bean>

 

 

需要把这个上下文定义为线程.主要还是对hibernate和spring不熟悉,花费了不少时间.

原创粉丝点击