实物配置如下
配置
希望该包名下的所有类的所有方法都启用事物,然后我在Service里面
红色除0出错,但是并没有回滚。
log如下:
<!-- 配置事务管理器 -->
<!-- ====================================== -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
<!-- <property name="rollbackOnCommitFailure" value="true" /> -->
</bean>
<!-- 注解方式配置事物 -->
<!-- ====================================== -->
<!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->
<!-- AOP方式配置事物 -->
<!-- ====================================== -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- <tx:method name="get*" propagation="REQUIRED" read-only="true" /> -->
<!-- <tx:method name="add*" propagation="REQUIRED" /> -->
<!-- <tx:method name="delete*" propagation="REQUIRED" /> -->
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="transactionPointcut"
expression="execution(* com.kl.napchen.store.impl..*.*(..))" />
<aop:advisor pointcut-ref="transactionPointcut"
advice-ref="transactionAdvice" />
</aop:config>
配置
expression="execution(* com.kl.napchen.store.impl..*.*(..))" />
希望该包名下的所有类的所有方法都启用事物,然后我在Service里面
@Override
public Map<String, String> delete(List<ProductType> productTypes) {
// 返回信息
Map<String, String> map = new HashMap<String, String>();
// hql参数
Map<String, Object> params;
// 用于返回计数
Long count = new Long(0);
// 实体类
ProductType productType;
// hql 语句
String hql = "";
System.out.println("size:" + productTypes.size());
try {
params = new HashMap<String, Object>();
for (int i = 0; i < productTypes.size(); i++) {
productType = productTypes.get(i);
System.out.println(productType.getType());
System.out.println(productType.getLargeType());
System.out.println(productType.getMediumType());
System.out.println(productType.getSmallType());
// 每一次循环清空参数
params.clear();
if (productType.getType().equals("大类")) {
// 检查大类下是否存在中类
hql = "SELECT COUNT (pt.type) FROM ProductType pt " + //
" WHERE pt.type = 'M' " + //
" AND pt.largeType = :largeType ";
params.put("largeType", productType.getLargeType());
count = productTypeDAO.count(hql, params);
System.out.println("count:" + count);
if (count > 0) {
map.put("ret", "-1");
map.put("error",
"当前大类下存在中类,不能删除!大类:"
+ productType.getLargeName());
return map;
}
}
if (productType.getType().equals("中类")) {
// 检查中类下是否存在小类
hql = "SELECT COUNT (*) FROM ProductType pt " + //
" WHERE pt.type = 'S' " + //
" AND pt.mediumType = :mediumType ";
params.put("mediumType", productType.getMediumType());
count = productTypeDAO.count(hql, params);
if (count > 0) {
map.put("ret", "-1");
map.put("error",
"当前中类下存在小类,不能删除!中类:"
+ productType.getMediumName());
return map;
}
}
// 小类不需要处理,直接删除
// productType.setLargeType(productTypes.get(i).getLargeType());
productType.setMediumType(productTypes.get(i).getMediumType()
.equals("-") ? null : productTypes.get(i)
.getMediumType());
productType.setSmallType(productTypes.get(i).getSmallType()
.equals("-") ? null : productTypes.get(i)
.getSmallType());
System.out.println(productType.getLargeType());
System.out.println(productType.getMediumType());
System.out.println(productType.getSmallType());
// 清空参数
params.clear();
// 根据三个类型可以确定ID
hql = " FROM ProductType pt" + //
" WHERE pt.largeType = :largeType ";//
params.put("largeType", productType.getLargeType());
if (productType.getMediumType() != null) {
hql = hql + " AND pt.mediumType = :mediumType ";
params.put("mediumType", productType.getMediumType());
}
if (productType.getSmallType() != null) {
hql = hql + " AND pt.smallType = :smallType ";
params.put("smallType", productType.getSmallType());
}
// 获取唯一的数据
productType = productTypeDAO.get(hql, params);
System.out.println("id:" + productType.getId());
productTypeDAO.delete(productType);
int g=1/0
}
map.put("ret", "0");
map.put("error", "");
return map;
} catch (Exception e) {
e.printStackTrace();
map.put("ret", "-1");
map.put("error", "Exception:" + e.getMessage());
return map;
}
}
红色除0出错,但是并没有回滚。
log如下:
Hibernate:
select
producttyp0_.id as id1_2_,
producttyp0_.type as type2_2_,
producttyp0_.large_name as large_na3_2_,
producttyp0_.large_type as large_ty4_2_,
producttyp0_.medium_name as medium_n5_2_,
producttyp0_.medium_type as medium_t6_2_,
producttyp0_.small_type as small_ty7_2_,
producttyp0_.small_name as small_na8_2_,
producttyp0_.create_time as create_t9_2_
from
t_product_type producttyp0_
where
producttyp0_.large_type=?
and producttyp0_.medium_type=?
id:24
java.lang.ArithmeticException: / by zero
at com.kl.napchen.store.impl.ProductTypeImpl.delete(ProductTypeImpl.java:128)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy22.delete(Unknown Source)
at com.kl.napchen.store.controller.ProductTypeController.deleteType(ProductTypeController.java:283)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:748)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:931)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:833)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
Hibernate:
delete
from
t_product_type
where
id=?