oraclejava8
oraclejava8
2012-03-26 19:11

Spring 事务管理,事物不回滚

  • it技术
  • 互联网问答
  • IT行业问题
  • 编程语言问答
  • 计算机技术

测试类:
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条回答

为你推荐

换一换