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个回答

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问