`
simen_net
  • 浏览: 300837 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

SpringMVC+OpenJPA奇怪的错误

 
阅读更多

在使用SpringDAO利用OpenJPA进行事务操作时出现错误代码(不使用事务的时候无错):

org.apache.openjpa.persistence.TransactionRequiredException: Can only perform operation while a transaction is active.

 

非常奇怪的一个错误提示,Google找到的类似问题都与我碰到的没有关系。

 

首先我有一个项目已经正常使用SpringMVC+OpenJPA,以此项目为基础新建了一个Ext-GWT的项目,修改了部分代码。

非常神奇的碰到了上面的问题,百思不得其解。顺着Spring代码往下找也一头雾水。

无奈之下,对两个项目进行逐步匹配修改…………居然问题是因为…………想死的心都有了。看代码如下:

 

applicationContextMVC.xml (SpringMVC的配置文件)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
  http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
	<!-- =====就是下面这句======== -->
	<context:component-scan base-package="com.strong.server" />

	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

	<mvc:interceptors>
		<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
		<!-- 解决延迟加载 -->
		<bean class="org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor">
			<property name="entityManagerFactory">
				<ref bean="entityManagerFactory" />
			</property>
		</bean>
	</mvc:interceptors>

	<!-- 启动Spring MVC的注解功能,完成请求和注解POJO -->
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="maxUploadSize" value="5000000000" />
		<property name="maxInMemorySize" value="4096" />
	</bean>

	<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />

</beans>

 

居然错在这里

<context:component-scan base-package="com.strong.server" />
 

我的Controller位置是:

package com.strong.server.module.controller;

 

用这个扫描包的时候就会出现最开始的错误,必须修改为:

<context:component-scan base-package="com.strong.server.module.**.controller" />
 

非常神奇的错误,但最主要的还是对SpringMVC理解的不够,貌似这方面的资料相对Struts来说实在太少。

顺便再推荐一下SpringMVC,它没有Struts那样的过度封装,可以在Controller直接调用原生的Request

可是实现程序最大程度的解耦合,强烈推荐之………………

 

 

 

 

 

 

分享到:
评论
1 楼 elvishehai 2012-02-10  
直接写也是可以的,不过要写全包名.

相关推荐

Global site tag (gtag.js) - Google Analytics