Spring OpenEntityManagerInViewFilter在数据库更新之前关闭了会话
我设置了OpenEntityManagerViewFilter,但它总是在写入数据库之前关闭会话。这是我的代码,请帮助我。谢谢Spring OpenEntityManagerInViewFilter在数据库更新之前关闭了会话,spring,jpa,transactions,Spring,Jpa,Transactions,我设置了OpenEntityManagerViewFilter,但它总是在写入数据库之前关闭会话。这是我的代码,请帮助我。谢谢 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>panshi</display-name>
<context-param>
<description>服务器的参数!</description>
<param-name>webAppRootKey</param-name>
<param-value>webRoot.root</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/*-config.xml
</param-value>
</context-param>
<context-param>
<description>日志的参数。</description>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.security.web.session.HttpSessionEventPublisher
</listener-class>
</listener>
<!--
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/picshow/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/CSS/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/images/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
-->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.json</url-pattern>
</servlet-mapping>
<filter>
<filter-name>httpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>httpMethodFilter</filter-name>
<servlet-name>spring</servlet-name>
</filter-mapping>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<session-config>
<session-timeout>10</session-timeout>
</session-config>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<!-- Displays a stack trace -->
<location>/WEB-INF/jsp/uncaughtException.jsp</location>
</error-page>
<filter>
<filter-name>SetCharacterEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>entityManagerFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>entityManagerFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>entityManagerFilter</filter-name>
<url-pattern>*.json</url-pattern>
</filter-mapping>
<!--
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class> org.springframework.orm.hibernate4.support.OpenSessionInViewFilter </filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>*.json</url-pattern>
</filter-mapping>
-->
<welcome-file-list>
<welcome-file>/index.html</welcome-file>
</welcome-file-list>
</web-app>
控制器中的方法
@Transactional
@RequestMapping(value="/admin/manage/resource.json",method = RequestMethod.POST)
@ResponseBody
public LinkedHashMap<String, Object> createResource(@RequestBody Resource resource){
Resource res = new Resource(resource.getResourceString());
res.setDescn(resource.getDescn());
res.setMethod(resource.getMethod());
res.setType(resource.getType());
res.setPriority(resource.getPriority());
resourceDAO.save(res);
if(!res.getId().isEmpty()){
return buildMessageResult(true,"成功建立资源"+res.getId());}
else{
return buildMessageResult(true,"建立资源失败!");
}
}
带有测试的输出
09-04 17:59:32 DEBUG RequestMappingHandlerMapping:220 - Looking up handler method for path /admin/manage/resource.json
09-04 17:59:32 DEBUG RequestMappingHandlerMapping:264 - Found 1 matching mapping(s) for [/admin/manage/resource.json] : [{[/admin/manage/resource.json],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}]
09-04 17:59:32 DEBUG HandlerMethod:135 - Method [initBinder] returned [null]
09-04 17:59:32 DEBUG HandlerMethod:129 - Invoking [createResource] method with arguments [cn.hballSoft.security.domain.Resource@56ad4264]
09-04 17:59:32 INFO ServerPU:60 - saving Resource instance
09-04 17:59:32 DEBUG TransactionSynchronizationManager:140 - Retrieved value [org.springframework.orm.jpa.EntityManagerHolder@1a08777c] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@529e0c79] bound to thread [http-8080-2]
09-04 17:59:32 DEBUG AbstractSaveEventListener:130 - Generated identifier: 40288ae840e869000140e869a3f90000, using strategy: org.hibernate.id.UUIDHexGenerator
09-04 17:59:32 INFO ServerPU:60 - save successful
09-04 17:59:32 DEBUG HandlerMethod:135 - Method [createResource] returned [{Success=true, Message=成功建立资源40288ae840e869000140e869a3f90000}]
09-04 17:59:32 DEBUG HandlerMethodReturnValueHandlerComposite:78 - Testing if return value handler [org.springframework.web.servlet.mvc.method.annotation.ModelAndViewMethodReturnValueHandler@e58ff51] supports [java.util.LinkedHashMap<java.lang.String, java.lang.Object>]
09-04 17:59:32 DEBUG HandlerMethodReturnValueHandlerComposite:78 - Testing if return value handler [org.springframework.web.method.annotation.ModelMethodProcessor@2b39d891] supports [java.util.LinkedHashMap<java.lang.String, java.lang.Object>]
09-04 17:59:32 DEBUG HandlerMethodReturnValueHandlerComposite:78 - Testing if return value handler [org.springframework.web.servlet.mvc.method.annotation.ViewMethodReturnValueHandler@6b6b4e30] supports [java.util.LinkedHashMap<java.lang.String, java.lang.Object>]
09-04 17:59:32 DEBUG HandlerMethodReturnValueHandlerComposite:78 - Testing if return value handler [org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor@59fcadd3] supports [java.util.LinkedHashMap<java.lang.String, java.lang.Object>]
09-04 17:59:32 DEBUG HandlerMethodReturnValueHandlerComposite:78 - Testing if return value handler [org.springframework.web.servlet.mvc.method.annotation.CallableMethodReturnValueHandler@31ccbefe] supports [java.util.LinkedHashMap<java.lang.String, java.lang.Object>]
09-04 17:59:32 DEBUG HandlerMethodReturnValueHandlerComposite:78 - Testing if return value handler [org.springframework.web.servlet.mvc.method.annotation.DeferredResultMethodReturnValueHandler@53e64a3b] supports [java.util.LinkedHashMap<java.lang.String, java.lang.Object>]
09-04 17:59:32 DEBUG HandlerMethodReturnValueHandlerComposite:78 - Testing if return value handler [org.springframework.web.servlet.mvc.method.annotation.AsyncTaskMethodReturnValueHandler@70f87478] supports [java.util.LinkedHashMap<java.lang.String, java.lang.Object>]
09-04 17:59:32 DEBUG HandlerMethodReturnValueHandlerComposite:78 - Testing if return value handler [org.springframework.web.method.annotation.ModelAttributeMethodProcessor@f74f6ef] supports [java.util.LinkedHashMap<java.lang.String, java.lang.Object>]
09-04 17:59:32 DEBUG HandlerMethodReturnValueHandlerComposite:78 - Testing if return value handler [org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor@47ffccd6] supports [java.util.LinkedHashMap<java.lang.String, java.lang.Object>]
09-04 17:59:32 DEBUG RequestResponseBodyMethodProcessor:150 - Written [{Success=true, Message=成功建立资源40288ae840e869000140e869a3f90000}] as "application/json;charset=UTF-8" using [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter@34a8a271]
09-04 17:59:32 DEBUG DispatcherServlet:999 - Null ModelAndView returned to DispatcherServlet with name 'spring': assuming HandlerAdapter completed request handling
09-04 17:59:32 DEBUG DispatcherServlet:1028 - Cleared thread-bound request context: FirewalledRequest[ org.apache.catalina.connector.RequestFacade@40545646]
09-04 17:59:32 DEBUG DispatcherServlet:966 - Successfully completed request
09-04 17:59:32 DEBUG XmlWebApplicationContext:332 - Publishing event in WebApplicationContext for namespace 'spring-servlet': ServletRequestHandledEvent: url=[/panshi/admin/manage/resource.json]; client=[127.0.0.1]; method=[POST]; servlet=[spring]; session=[1AC287B1EBE796642F17B9AB4F9B6D8A]; user=[null]; time=[96ms]; status=[OK]
09-04 17:59:32 DEBUG XmlWebApplicationContext:332 - Publishing event in Root WebApplicationContext: ServletRequestHandledEvent: url=[/panshi/admin/manage/resource.json]; client=[127.0.0.1]; method=[POST]; servlet=[spring]; session=[1AC287B1EBE796642F17B9AB4F9B6D8A]; user=[null]; time=[96ms]; status=[OK]
09-04 17:59:32 DEBUG TransactionSynchronizationManager:243 - Removed value [org.springframework.orm.jpa.EntityManagerHolder@1a08777c] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@529e0c79] from thread [http-8080-2]
09-04 17:59:32 DEBUG OpenEntityManagerInViewFilter:188 - Closing JPA EntityManager in OpenEntityManagerInViewFilter
09-04 17:59:32 DEBUG EntityManagerFactoryUtils:338 - Closing JPA EntityManager
09-04 18:09:59 DEBUG HttpSessionEventPublisher:83 - Publishing event: org.springframework.security.web.session.HttpSessionDestroyedEvent[source=org.apache.catalina.session.StandardSessionFacade@75f10df7]
09-04 18:09:59 DEBUG XmlWebApplicationContext:332 - Publishing event in Root WebApplicationContext: org.springframework.security.web.session.HttpSessionDestroyedEvent[source=org.apache.catalina.session.StandardSessionFacade@75f10df7]
09-04 18:09:59 DEBUG DefaultListableBeanFactory:246 - Returning cached instance of singleton bean 'org.springframework.security.core.session.SessionRegistryImpl#0'
09-04 18:09:59 DEBUG DefaultListableBeanFactory:246 - Returning cached instance of singleton bean 'loggerListener'
09-04 18:44:05 DEBUG EntityManagerFactoryRegistry:66 - Registering EntityManagerFactory: ServerPU
09-04 18:44:05 WARN EntityManagerFactoryRegistry:80 - HHH000436: Entity manager factory name (ServerPU) is already registered. If entity manager will be clustered or passivated, specify a unique value for property 'hibernate.ejb.entitymanager_factory_name'
09-04 18:44:05 INFO ServerPU:60 - saving Resource instance
09-04 18:44:05 DEBUG TransactionSynchronizationManager:140 - Retrieved value [org.springframework.orm.jpa.EntityManagerHolder@4b5a142f] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@6e3404f] bound to thread [main]
[org.springframework.transaction.support.DefaultTransactionStatus@54eeabe8]; rollback [false]
09-04 18:44:05 DEBUG JpaTransactionManager:923 - Triggering beforeCommit synchronization
09-04 18:44:05 DEBUG JpaTransactionManager:936 - Triggering beforeCompletion synchronization
09-04 18:44:05 DEBUG JpaTransactionManager:753 - Initiating transaction commit
09-04 18:44:05 DEBUG JpaTransactionManager:508 - Committing JPA transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@62c37f31]
09-04 18:44:05 DEBUG AbstractTransactionImpl:173 - committing
09-04 18:44:05 DEBUG JdbcTransaction:113 - committed JDBC Connection
09-04 18:44:05 DEBUG JdbcTransaction:126 - re-enabling autocommit
09-04 18:44:05 DEBUG DirtiesContextTestExecutionListener:107 - After test method: context [[TestContext@2f8bbc98 testClass = TestJPA, testInstance = cn.hballSoft.test.TestJPA@6ba7508a, testMethod = modifyDatabaseWithinTransaction@TestJPA, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@3598cb3d testClass = TestJPA, locations = '{classpath:/cn/hballSoft/test/jpa-config.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]]], class dirties context [false], class mode [null], method dirties context [false].
09-04 18:44:05 INFO GenericApplicationContext:1042 - Closing org.springframework.context.support.GenericApplicationContext@4cf7c31d: startup date [Wed Sep 04 18:43:59 CST 2013]; root of context hierarchy
09-04 18:44:05 DEBUG GenericApplicationContext:332 - Publishing event in org.springframework.context.support.GenericApplicationContext@4cf7c31d: org.springframework.context.event.ContextClosedEvent[source=org.springframework.context.support.GenericApplicationContext@4cf7c31d: startup date [Wed Sep 04 18:43:59 CST 2013]; root of context hierarchy]
09-04 18:44:05 DEBUG DefaultListableBeanFactory:246 - Returning cached instance of singleton bean 'lifecycleProcessor'
09-04 18:44:05 INFO DefaultListableBeanFactory:444 - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@62770d2e: defining beans
09-04 18:44:05 DEBUG DisposableBeanAdapter:226 - Invoking destroy() on bean with name 'entityManagerFactory'
09-04 18:44:05 INFO LocalContainerEntityManagerFactoryBean:441 - Closing JPA EntityManagerFactory for persistence unit 'ServerPU'
09-04 18:44:05 DEBUG SessionFactoryImpl:1369 - HHH000031: Closing
09-04 18:44:05 DEBUG EntityManagerFactoryRegistry:108 - Remove: name=ServerPU
09-04 18:44:05 DEBUG DisposableBeanAdapter:302 - Invoking destroy method 'close' on bean with name 'dataSource'
我知道会发生什么。
我有多个*-config.xml
他们都有
<context:annotation-config />
<context:component-scan base-package="cn.hballSoft.domain" />
<context:component-scan base-package="cn.hballSoft.security.domain" />
可能会导致重复加载事务配置
保持单身,解决问题。我知道会发生什么。
我有多个*-config.xml
他们都有
<context:annotation-config />
<context:component-scan base-package="cn.hballSoft.domain" />
<context:component-scan base-package="cn.hballSoft.security.domain" />
可能会导致重复加载事务配置
保持单身,解决问题
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("jpa-config.xml")
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=false)
@Transactional
public class TestJPA {
ApplicationContext ctx;
DataSource dataSource;
@BeforeTransaction
public void verifyInitialDatabaseState() {
ctx = new FileSystemXmlApplicationContext("E:/FBworkspace/panshi/WebContent/WEB-INF/spring/jpa-config.xml");
dataSource = (DataSource)ctx.getBean("dataSource");
}
@Before
public void setUpTestDataWithinTransaction() {
// set up test data within the transaction
}
@Test
// overrides the class-level defaultRollback setting
@Rollback(false)
public void modifyDatabaseWithinTransaction() {
Resource res = new Resource("TEset REsource");
ResourceDAO resDao = (ResourceDAO)ctx.getBean("resourceDAO");
resDao.save(res);
}
@After
public void tearDownWithinTransaction() {
// execute "tear down" logic within the transaction
}
@AfterTransaction
public void verifyFinalDatabaseState() {
// logic to verify the final state after transaction has rolled back
}
}
09-04 18:44:05 DEBUG EntityManagerFactoryRegistry:66 - Registering EntityManagerFactory: ServerPU
09-04 18:44:05 WARN EntityManagerFactoryRegistry:80 - HHH000436: Entity manager factory name (ServerPU) is already registered. If entity manager will be clustered or passivated, specify a unique value for property 'hibernate.ejb.entitymanager_factory_name'
09-04 18:44:05 INFO ServerPU:60 - saving Resource instance
09-04 18:44:05 DEBUG TransactionSynchronizationManager:140 - Retrieved value [org.springframework.orm.jpa.EntityManagerHolder@4b5a142f] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@6e3404f] bound to thread [main]
[org.springframework.transaction.support.DefaultTransactionStatus@54eeabe8]; rollback [false]
09-04 18:44:05 DEBUG JpaTransactionManager:923 - Triggering beforeCommit synchronization
09-04 18:44:05 DEBUG JpaTransactionManager:936 - Triggering beforeCompletion synchronization
09-04 18:44:05 DEBUG JpaTransactionManager:753 - Initiating transaction commit
09-04 18:44:05 DEBUG JpaTransactionManager:508 - Committing JPA transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@62c37f31]
09-04 18:44:05 DEBUG AbstractTransactionImpl:173 - committing
09-04 18:44:05 DEBUG JdbcTransaction:113 - committed JDBC Connection
09-04 18:44:05 DEBUG JdbcTransaction:126 - re-enabling autocommit
09-04 18:44:05 DEBUG DirtiesContextTestExecutionListener:107 - After test method: context [[TestContext@2f8bbc98 testClass = TestJPA, testInstance = cn.hballSoft.test.TestJPA@6ba7508a, testMethod = modifyDatabaseWithinTransaction@TestJPA, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@3598cb3d testClass = TestJPA, locations = '{classpath:/cn/hballSoft/test/jpa-config.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]]], class dirties context [false], class mode [null], method dirties context [false].
09-04 18:44:05 INFO GenericApplicationContext:1042 - Closing org.springframework.context.support.GenericApplicationContext@4cf7c31d: startup date [Wed Sep 04 18:43:59 CST 2013]; root of context hierarchy
09-04 18:44:05 DEBUG GenericApplicationContext:332 - Publishing event in org.springframework.context.support.GenericApplicationContext@4cf7c31d: org.springframework.context.event.ContextClosedEvent[source=org.springframework.context.support.GenericApplicationContext@4cf7c31d: startup date [Wed Sep 04 18:43:59 CST 2013]; root of context hierarchy]
09-04 18:44:05 DEBUG DefaultListableBeanFactory:246 - Returning cached instance of singleton bean 'lifecycleProcessor'
09-04 18:44:05 INFO DefaultListableBeanFactory:444 - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@62770d2e: defining beans
09-04 18:44:05 DEBUG DisposableBeanAdapter:226 - Invoking destroy() on bean with name 'entityManagerFactory'
09-04 18:44:05 INFO LocalContainerEntityManagerFactoryBean:441 - Closing JPA EntityManagerFactory for persistence unit 'ServerPU'
09-04 18:44:05 DEBUG SessionFactoryImpl:1369 - HHH000031: Closing
09-04 18:44:05 DEBUG EntityManagerFactoryRegistry:108 - Remove: name=ServerPU
09-04 18:44:05 DEBUG DisposableBeanAdapter:302 - Invoking destroy method 'close' on bean with name 'dataSource'
<context:annotation-config />
<context:component-scan base-package="cn.hballSoft.domain" />
<context:component-scan base-package="cn.hballSoft.security.domain" />