我需要帮助,我通过扩展使用自定义opensessioninviewfilter,我重写了doFilterInternal方法,我将sessionFactory存储在静态ThredLocal变量中:
public static ThreadLocal<SessionFactory> current = new ThreadLocal<>();
在我的dao中,我检查了sessionFactory是否与我的Filter中使用的相同,我意识到不是同一个sessionFactory,有两个不同的实例,
DAO:
@Autowired
protected SessionFactory sessionFactory;
opensessionViewFilter.doFilterInternal MyClass.current.set(session);
我的服务层:
@Service("comiteService")
@Transactional
public class CommiteServiceImpl implements CommiteService{
@Autowired
CommiteDao dao;
Web.xml:
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.web.filter.MyOpenViewInSession</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
applicationContext.xml:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="org.entity"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql:true}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql:true}</prop>
<prop key="hibernate.cache.use_second_level_cache" >false</prop>
<prop key="hibernate.cache.region.factory_class" >org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<prop key="org.hibernate logging">ALL</prop>
<prop key="net.sf.Ehcache.configurationResourceName">/Ehcache.xml</prop>
<!-- <prop key="hibernate.enable_lazy_load_no_trans"> true</prop> -->
<prop key="spring.jpa.properties.hibernate.generate_statistics" >true</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
</bean>
在我的DAO中,我执行以下操作以检查sessionFactory是否与filter相同或不同:
if(System.identityHashCode(getSessionFactory()) == System.identityHashCode(MyClass.current))
System.out.println("sessionfactory is same");
else System.out.println("SessionFactory are not same");
输出为:“ SessionFactory不相同”