oraclejava8 2012-03-26 19:11
浏览 270
已采纳

Spring 事务管理,事物不回滚

测试类:
package com.demo.test;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.demo.entity.DLog;
import com.demo.entity.User;
import com.demo.service.LogService;
import com.demo.service.UserService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "../../../applicationContext.xml" })
public class DemoTest {

@Resource
UserService userService;
@Resource
LogService logService;

@Test
public void testUserService() {
DLog dLog = new DLog("saved!");
logService.add(dLog);
User user = new User();
user.setUserId(1);

//这里删除的时候 我设定了一个不存在的值 删除不成功 抛出异常 但是第一个logService.add(dLog);
志 已经插入了,并且没有回滚
userService.delete(user);
}

spring配置文件:

org.hibernate.dialect.MySQLDialect
updatetruecom.demo.entity

logDao:

package com.demo.daoimpl;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.demo.dao.LogDao;
import com.demo.dao.UserDao;
import com.demo.entity.DLog;
import com.demo.entity.User;

@Repository
public class LogDaoImpl implements LogDao {

Log log = LogFactory.getLog(LogDaoImpl.class);
@Resource
SessionFactory sessionFactory;

public void add(DLog dLog) {
Session session = sessionFactory.getCurrentSession();
session.save(dLog);
}

public void delete(DLog dLog) {
Session session = sessionFactory.getCurrentSession();
session.delete(dLog);
}
}

userDao:

package com.demo.daoimpl;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.demo.dao.UserDao;
import com.demo.entity.User;

@Repository
public class UserDaoImpl implements UserDao {

Log log = LogFactory.getLog(UserDaoImpl.class);
@Resource
SessionFactory sessionFactory;

public void add(User user) {
Session session = sessionFactory.getCurrentSession();
session.save(user);
}

public void delete(User user) {
Session session = sessionFactory.getCurrentSession();
session.delete(user);
}
}

logService:

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.demo.dao.LogDao;
import com.demo.dao.UserDao;
import com.demo.entity.DLog;
import com.demo.entity.User;
import com.demo.service.LogService;
import com.demo.service.UserService;

@Service
@Transactional
public class LogServiceImpl implements LogService {

@Resource
LogDao logDao;

@Transactional()
public void add(DLog dLog) {
logDao.add(dLog);
}

@Transactional()
public void delete(DLog dLog) {
logDao.delete(dLog);
}
}

userService:

package com.demo.serviceimpl;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.demo.dao.UserDao;
import com.demo.entity.User;
import com.demo.service.UserService;

@Service
@Transactional
public class UserServiceImpl implements UserService {

@Resource
UserDao userDao;

@Transactional()
public void add(User user) {
userDao.add(user);
}

@Transactional()
public void delete(User user) {
userDao.delete(user);
}
}

  • 写回答

1条回答 默认 最新

  • iteye_9219 2012-03-28 17:16
    关注

    两个service是单独的事物,如果你把testUserService增加事物应该能回滚

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。
  • ¥40 宿舍管理系统设计(c#)
  • ¥15 MATLAB卫星二体模型仿真
  • ¥15 怎么让数码管亮的同时让led执行流水灯代码
  • ¥20 SAP HANA SQL Script 。如何判断字段值包含某个字符串
  • ¥85 cmd批处理参数如果含有双引号,该如何传入?
  • ¥15 fx2n系列plc的自控成型机模拟
  • ¥15 时间序列LSTM模型归回预测代码问题
  • ¥50 使用CUDA如何高效的做并行化处理,是否可以多个分段同时进行匹配计算处理?目前数据传输速度有些慢,如何提高速度,使用gdrcopy是否可行?请给出具体意见。