Spring+hibernate事务不提交,为什么
1个回答
展开全部
一、配置如下:
1)applicationContext-common.xml
Java代码
<?xml version="1.0" encoding="UTF-8"?>
<!-- 加载jdbc配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 配置数据源dataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="validationQuery" value="SELECT 1" />
<property name="testWhileIdle" value="true" />
<property name="timeBetweenEvictionRunsMillis" value="3600000" />
<property name="minEvictableIdleTimeMillis" value="18000000" />
<property name="testOnBorrow" value="true" />
</bean>
<!-- 配置SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.jdbc.fatch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
</props>
</property>
<property name="mappingLocations">
<list>
<value>WEB-INF/classes/pojo/*.hbm.xml</value>
</list>
</property>
</bean>
<!-- 扫描service目录,把作了注解的类转换为bean -->
<context:component-scan base-package="mod.service" />
<!-- 声明式事务 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<!-- 配置事务属性 -->
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_SUPPORTS,readOnly</prop>
<prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 注册com.base.HibernateDAO -->
<bean class="com.base.HibernateDAO" >
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
2、spring-servlet.xml
Java代码 收藏代码
<?xml version="1.0" encoding="UTF-8"?>
<!-- 扫描action目录,把作了注解的类转换为bean -->
<context:component-scan base-package="mod.action" />
<!-- <context:annotation-config /> -->
<!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/view/" p:suffix=".jsp" />
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:defaultEncoding="utf-8" />
<!-- 自动注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个实例 -->
<mvc:annotation-driven />
<mvc:interceptors>
<!--openSessionInViewInterceptor -->
<bean id="openSessionInViewInterceptor"
class="org.springframework.orm.hibernate4.support.OpenSessionInViewInterceptor">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<mvc:interceptor>
<mvc:mapping path="/*" />
<bean id="authInterceptor" class="com.security.AuthInterceptor" />
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/*" />
<bean id="timerInterceptor" class="com.security.TimerInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
</beans>
二、相关代码如下:
1)action中的删除操作
Java代码
/**
* 删除记录
*
* @param id
*/
@RequestMapping(value = "log_delete")
public String delete(HttpServletRequest request, Long id) {
if (id != null) {
Eq.println("============^_^=============执行删除操作,id=" + id);
logService.delete(id);
Eq.println("============^_^=============操作执行结束=======");
}
Eq.println("**********地址来源=" + Eq.getReferer(request));
return "redirect:log_list";
}
2)service中的删除操作
Java代码 收藏代码
/**
* 根据主键删除实体
*/
@Override
public void delete(Long id) {
dao.delete(Log.class, id);
//dao.flush();
}
3)dao中的删除操作
Java代码 收藏代码
public boolean delete(final Class<?> clazz, final Long id) {
Assert.notNull(id, "id不能为空");
try {
getSession().delete(getSession().load(clazz, id));
logger.debug("delete entity {},id is {}", clazz.getSimpleName(), id);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
1)applicationContext-common.xml
Java代码
<?xml version="1.0" encoding="UTF-8"?>
<!-- 加载jdbc配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 配置数据源dataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="validationQuery" value="SELECT 1" />
<property name="testWhileIdle" value="true" />
<property name="timeBetweenEvictionRunsMillis" value="3600000" />
<property name="minEvictableIdleTimeMillis" value="18000000" />
<property name="testOnBorrow" value="true" />
</bean>
<!-- 配置SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.jdbc.fatch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
</props>
</property>
<property name="mappingLocations">
<list>
<value>WEB-INF/classes/pojo/*.hbm.xml</value>
</list>
</property>
</bean>
<!-- 扫描service目录,把作了注解的类转换为bean -->
<context:component-scan base-package="mod.service" />
<!-- 声明式事务 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<!-- 配置事务属性 -->
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_SUPPORTS,readOnly</prop>
<prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 注册com.base.HibernateDAO -->
<bean class="com.base.HibernateDAO" >
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
2、spring-servlet.xml
Java代码 收藏代码
<?xml version="1.0" encoding="UTF-8"?>
<!-- 扫描action目录,把作了注解的类转换为bean -->
<context:component-scan base-package="mod.action" />
<!-- <context:annotation-config /> -->
<!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/view/" p:suffix=".jsp" />
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:defaultEncoding="utf-8" />
<!-- 自动注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个实例 -->
<mvc:annotation-driven />
<mvc:interceptors>
<!--openSessionInViewInterceptor -->
<bean id="openSessionInViewInterceptor"
class="org.springframework.orm.hibernate4.support.OpenSessionInViewInterceptor">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<mvc:interceptor>
<mvc:mapping path="/*" />
<bean id="authInterceptor" class="com.security.AuthInterceptor" />
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/*" />
<bean id="timerInterceptor" class="com.security.TimerInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
</beans>
二、相关代码如下:
1)action中的删除操作
Java代码
/**
* 删除记录
*
* @param id
*/
@RequestMapping(value = "log_delete")
public String delete(HttpServletRequest request, Long id) {
if (id != null) {
Eq.println("============^_^=============执行删除操作,id=" + id);
logService.delete(id);
Eq.println("============^_^=============操作执行结束=======");
}
Eq.println("**********地址来源=" + Eq.getReferer(request));
return "redirect:log_list";
}
2)service中的删除操作
Java代码 收藏代码
/**
* 根据主键删除实体
*/
@Override
public void delete(Long id) {
dao.delete(Log.class, id);
//dao.flush();
}
3)dao中的删除操作
Java代码 收藏代码
public boolean delete(final Class<?> clazz, final Long id) {
Assert.notNull(id, "id不能为空");
try {
getSession().delete(getSession().load(clazz, id));
logger.debug("delete entity {},id is {}", clazz.getSimpleName(), id);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询