Java – spring opensessioninview filter with @ transactional annotation

This is about spring opensessioninview filter using @ transactional annotation in the service layer

I've experienced so many stack overflow posts, but I'm still confused about whether I should use opensessioninview filter or avoid lazyinitializationexception. If someone helps me find the answer to the following query, it will be very helpful

>Using opensessioninview filter in an application is a bad practice with complex patterns. > Using this filter may cause n 1 problems > if we use opensessioninview filter, does it mean @ transactional is not required?

Here is my spring configuration file

<context:component-scan base-package="com.test"/>
<context:annotation-config/>
 <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="resources/messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>
 <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="/WEB-INF/jdbc.properties" />
 <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
        p:driverClassName="${jdbc.driverClassName}"
        p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
        p:password="${jdbc.password}" />
       <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionfactorybean">
        <property name="dataSource" ref="dataSource" />     
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                <prop key="hibernate.show_sql">true</prop>
                <!--
                <prop key="hibernate.hbm2ddl.auto">create</prop>
                 -->
            </props>
        </property>
    </bean>
 <tx:annotation-driven /> 
 <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />

  </bean>

Solution

Opensessioninview is a servlet filter, rather than just opening a hibernate session and storing it in the sessionholder of the thread requesting the service After opening this session, hibernate can read lazy initialized collections and objects when you use this in the rendering phase of the request Call sessionfactory This session can be accessed when getcurrentsession()

However, opensessioninview only opens a session and does not start any transactions After opening a session, you can read objects from the database, but if you want to perform some operations in a transaction, you need to use @ transactional annotation or other mechanisms to divide at the beginning and end of the transaction

Then the answer to the question is:

This is a good practice if you need to avoid lazyinitializationexception, and overload just opens a new hibernate session and closes it at the end of each request

I use this filter in many projects without any problems

No, you only have one hibernate session to open the thread in the sessionholder, but if you need a transaction, you need to put @ transactional

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>