我正在使用 OpenSessionInViewFilter、Spring 和 Hibernate 进行数据访问。当我提交数据记录时,我的 View 结果不显示使用延迟加载的提交记录。我的 session 工厂配置如下所示:
<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>
OpenSessionInViewFilter web.xml 中的配置看起来完全像这样:
<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 获取 session ,我有下面的 SessionLocator 类,它为我打开 session ;
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()
时,我可以看到表中的记录,但更新后的值在延迟加载期间不会显示在我的 View 中。有谁知道为什么 hibernate 缓存没有用当前记录刷新?
最佳答案
此问题已通过从 openSessionInViewFilter 配置中删除以下行得到解决:
<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;
}
关于java - 加载的记录不是最新的。使用 Hibernate 和 OpenSessionInViewFilter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4302983/