Java Spring Hibernate AOP上下文的OpenSessionInterceptor
我正在用Spring4、JSF2和Hibernate5开发一个web应用程序。为了在web上下文中管理hibernate中的事务操作,我在web.xml中使用OpenSessionInViewFilterJava Spring Hibernate AOP上下文的OpenSessionInterceptor,java,spring,hibernate,spring-aop,transactional,Java,Spring,Hibernate,Spring Aop,Transactional,我正在用Spring4、JSF2和Hibernate5开发一个web应用程序。为了在web上下文中管理hibernate中的事务操作,我在web.xml中使用OpenSessionInViewFilter <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate5.suppor
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
我需要在spring上下文事件侦听器中使用事务,因此我不能使用OpenSessionInViewFilter,因为它只适用于web请求。我想我可以将OpenSessionInterceptor与AOP一起使用,但我没有找到任何使用它的例子
如何将OpenSessionInterceptor与AOP一起使用?OpenSessionView与事务无关……我很肯定你错了。我很确定我没有,请阅读您指向的文档的第一行,这是
OpenSessionInViewFilter
的目的。视图中打开会话的目的只是保持会话处于打开状态,这样就不会出现延迟加载异常。。。它与事务或开始/结束事务无关。。。如果您需要侦听器中的事务,请将其设置为事务性的…Deinum,对不起,我不想粗鲁无礼。我更新了答案,以便更好地解释我的问题。如果出现此异常,则与使用视图中的打开会话筛选器无关。它基本上指出您设置的事务是错误的。为什么要使用mode=“aspectj”
这意味着您正在使用加载或编译时编织来应用您的方面,但是
建议使用其他方式。。。
<bean id="dataSource" class="com.p6spy.engine.spy.P6DataSource">
<constructor-arg>
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
</constructor-arg>
</bean>
<!-- Hibernate 5 Annotation SessionFactory Bean definition -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>org.dummy.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.flushMode">COMMIT</prop>
<prop key="org.hibernate.flushMode">COMMIT</prop>
</props>
</property>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<context:annotation-config />
<context:component-scan base-package="org.dummy.persistence" />
<aop:aspectj-autoproxy />
<tx:annotation-driven mode="aspectj" transaction-manager="txManager" />
@Component
public class SimplePopulator implements ApplicationListener<ContextRefreshedEvent> {
@Autowired
protected UserFacadeService userFacadeService;
@Autowired
protected SessionFactory sessionFactory;
@Override
@Transactional
public void onApplicationEvent(ContextRefreshedEvent event) {
try {
////sessionFactory.set
//Session session = sessionFactory.openSession();
//session.beginTransaction();
@SuppressWarnings("unused")
UserAccount account1 = userFacadeService.createUserAccount("testAA", "testAA", "User Dummy", "dummy@email.org");
//session.save(account1);
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
System.out.println(userFacadeService.existsAccount("test"));
//session.getTransaction().commit();
//session.close();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
at org.springframework.orm.hibernate5.SpringSessionContext.currentSession(SpringSessionContext.java:132)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:692)
at org.abubu.whisper.persistence.impl.AbstractRepository.getCurrentSession(AbstractRepository.java:46)
at org.abubu.whisper.persistence.impl.UserAccountRepositoryImpl.findByUsername(UserAccountRepositoryImpl.java:26)