我正在使用OpenSessionInViewFilter,Spring和Hibernate进行数据访问。当我提交数据记录时,我的视图结果不显示使用延迟加载的提交记录。我的会话工厂配置如下所示:
<bean id="dbcpDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.user}" />
<property name="password" value="${database.password}" />
<property name="poolPreparedStatements" value="true" />
<property name="defaultAutoCommit" value="true" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dbcpDataSource" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dbcpDataSource" />
<property name="annotatedClasses">
<list>
<value>or.pac.Address</value>
<value>or.pac.Customer</value>
<value>or.pac.Documents</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.connection.release_mode">auto</prop>
<prop key="hibernate.current_session_context_class">jta</prop>
<prop key="hibernate.transaction.auto_close_session">true</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.generate_statistics">false</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.max_fetch_depth">4</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="locator" class="cc.SessionLocator">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
web.xml中的OpenSessionInViewFilter 配置看起来完全像这样:
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>flushMode</param-name>
<param-value>AUTO</param-value>
</init-param>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
要获取我DAO的会话,我有以下SessionLocator类,它会为我打开会话;
public class SessionLocator{
public static final ThreadLocal session = new ThreadLocal();
private static SessionLocator me;
static {
try {
me = new SessionLocator();
} catch (Exception e) {
e.printStackTrace();
}
}
private SessionLocator() throws HibernateException, JDBCException {
}
public static Session currentSession() throws Exception {
Session s = (Session) session.get();
if (s != null) {
s.setCacheMode(CacheMode.REFRESH);
}
if (s == null) {
synchronized SessionLocator{
s =openSession();
}
session.set(s);
}
return s;
}
public void setSessionFactory(SessionFactory sessionFactory) {
PersistenceManager.sessionFactory = sessionFactory;
}
public static Session openSession() {
Session session = SessionFactoryUtils.doGetSession(sessionFactory, true);
return session;
}
private static SessionFactory sessionFactory;
}
然后在我的DAO服务中,我像这样保存并更新:
public boolean upsert(Object d) {
try {
SessionLocator.currentSession().saveOrUpdate(d);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
当我调用upsert()
时,我可以看到表上的记录,但是在延迟加载期间,我的视图中没有显示更新的值。有人知道为什么不用当前记录刷新hibernate缓存吗?
答案 0 :(得分:1)
通过从openSessionInViewFilter config中删除以下行来解决此问题:
<dispatcher>FORWARD</dispatcher>
使用@Transactional注释
注释DAO服务@Transactional
public boolean upsert(Object d) {
try {
SessionLocator.currentSession().saveOrUpdate(d);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}