论坛首页 Java企业应用论坛

spring + hibernate 申明式事务不起作用

浏览 13835 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-04-18  
<?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:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

	<!-- 自动扫描且只扫描@Controller -->
	<context:component-scan base-package="com.zls.examples.miniweb.web">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	
	<mvc:annotation-driven />
	
	<mvc:view-controller path="/" view-name="redirect:/account/user/"/>
	
	<mvc:resources mapping="/static/**" location="/static/" />
	
	<mvc:default-servlet-handler/>
	 
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
</beans>
0 请登录后投票
   发表时间:2012-04-18  
能否把另一个配置文件发下看看
0 请登录后投票
   发表时间:2012-04-18  
配了切入点吗?
0 请登录后投票
   发表时间:2012-04-18  
yanbo 写道
配了切入点吗?

他用的是注解
0 请登录后投票
   发表时间:2012-04-18  
<?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:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
		"
	default-lazy-init="true">

	<description>Spring公共配置 </description>

	<!-- 使用annotation 自动注册bean, 并保证@Required、@Autowired的属性被注入 -->
	<context:component-scan base-package="com.zls.examples.miniweb">
		<context:exclude-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
	</context:component-scan>

	<!-- Hibernate配置 -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="namingStrategy">
			<bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
				<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
				<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
				<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
				<prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache/ehcache-hibernate-local.xml</prop>
			</props>
		</property>

		<property name="packagesToScan" value="com.zls.examples.miniweb.entity" />
	</bean>


	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
		<property name="ignoreResourceNotFound" value="true" />
		<property name="locations">
			<list>
				<value>classpath*:/application.properties</value>
			</list>
		</property>
	</bean>

	<!-- 数据源配置,使用应用内的DBCP数据库连接池 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close" lazy-init="false">
		<!-- Connection Info -->
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />

		<!-- Connection Pooling Info -->
		<property name="maxIdle" value="${dbcp.maxIdle}" />
		<property name="maxActive" value="${dbcp.maxActive}" />
		<property name="defaultAutoCommit" value="false" />
		<property name="timeBetweenEvictionRunsMillis" value="3600000" />
		<property name="minEvictableIdleTimeMillis" value="3600000" />
	</bean>


	<!-- 事务管理器配置,单数据源事务 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<!-- 使用annotation定义事务 -->
	<context:annotation-config />
	<tx:annotation-driven transaction-manager="transactionManager" />

</beans>
0 请登录后投票
   发表时间:2012-04-18  
你是不是 直接 Controller调用了DAO啊
0 请登录后投票
   发表时间:2012-04-18  
jinnianshilongnian 写道
你是不是 直接 Controller调用了DAO啊

没有,分层的。下面是controller 和 service的代码
@Controller
public class OpenController {
	private AccountManager accountManager;
	
	@RequestMapping(value = "/open/save.do")
	public String list(Integer pageNo,Model model,HttpServletRequest request) {
		
		User user = new User();
		user.setLoginName("kimi");
		user.setPassword("abc");
		user.setName("kimimo");
		user.setSalt("1111");
		user.setEmail("ben@zls.cn");
		accountManager.saveUser(user);
		return "redirect:/login";
	}
	@Autowired
	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}
}


@Component
@Transactional
public class AccountManager {

	private UserDao userDao;
	)
	public void saveUser(User entity) {
	
		userDao.save(entity);
		
	}


	@Autowired
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
}
0 请登录后投票
   发表时间:2012-04-18  
@Transactional(readOnly = true) 
public class UserManager { 


改成

@Transactional试试
public class UserManager { 
0 请登录后投票
   发表时间:2012-04-18  
clean下项目

执行 如果有问题 请把完整的日志作为附件上传上来 分析下原因
0 请登录后投票
   发表时间:2012-04-18   最后修改:2012-04-18
因为OpenSessionInViewFilter中Session的FlushModel默认是 MANUAL,,因此如果service层没有实施事务的话 是不会commit的。

当然也可以通过在web.xml中设置(只有在没有服务层事务时才设置)
<init-param>
   <param-name>flushMode</param-name>
   <param-value>AUTO</param-value>
</init-param>

这里说明你的服务层事务没有实施成功

AopUtils.isAopProxy(userManager)  看看 如果true表示AOP代理成功 否则失败,检查配置文件
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics