Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

整合mybatis事务失效 #27

Closed
wi1wei opened this issue May 17, 2018 · 1 comment
Closed

整合mybatis事务失效 #27

wi1wei opened this issue May 17, 2018 · 1 comment

Comments

@wi1wei
Copy link

wi1wei commented May 17, 2018

Application

@SpringBootApplication
@MapperScan(basePackages = {"org.inlighting.dao"})
@EnableTransactionManagement
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application .class);
    }
}

Service

public interface IUserService {
     UserBean getUser(String username) ;
}

ServiceImpl

@Service
public class UserService implements IUserService{
    private final UserMapper mapper;

    @Autowired
    public UserService(UserMapper mapper) {
        this.mapper = mapper;
    }

    @Transactional(rollbackFor = Exception.class)
    public UserBean getUser(String username) {
        // 没有此用户直接返回null
        Map<String, String> detail = mapper.selectUserByName(username);
        if (detail == null )
            return null;

        UserBean user = new UserBean();
        user.setUsername(username);
        user.setPassword(detail.get("USER_PSWD"));
        user.setRole(detail.get("ROLE_NAME"));
        user.setPermission("*");
        return user;
    }
}

log

2018-05-17 10:51:12.517 DEBUG 9948 --- [nio-1024-exec-1] org.mybatis.spring.SqlSessionUtils       : Creating a new SqlSession
2018-05-17 10:51:12.520 DEBUG 9948 --- [nio-1024-exec-1] org.mybatis.spring.SqlSessionUtils       : SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@57f320e4] was not registered for synchronization because synchronization is not active
2018-05-17 10:51:12.856  INFO 9948 --- [nio-1024-exec-1] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited
2018-05-17 10:51:12.860 DEBUG 9948 --- [nio-1024-exec-1] o.i.dao.UserMapper.selectUserByName      : ==>  Preparing: select * from user where userName = ?
2018-05-17 10:51:12.882 DEBUG 9948 --- [nio-1024-exec-1] o.i.dao.UserMapper.selectUserByName      : ==> Parameters: ricky(String)
2018-05-17 10:51:12.905 DEBUG 9948 --- [nio-1024-exec-1] o.i.dao.UserMapper.selectUserByName      : <==      Total: 1
2018-05-17 10:51:12.906 DEBUG 9948 --- [nio-1024-exec-1] org.mybatis.spring.SqlSessionUtils       : Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@57f320e4]

删除 DefaultAdvisorAutoProxyCreator 配置就正常

2018-05-17 11:02:05.302 DEBUG 7380 --- [nio-1024-exec-1] org.mybatis.spring.SqlSessionUtils       : Creating a new SqlSession
2018-05-17 11:02:05.307 DEBUG 7380 --- [nio-1024-exec-1] org.mybatis.spring.SqlSessionUtils       : Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6c7ef00b]
2018-05-17 11:02:05.320 DEBUG 7380 --- [nio-1024-exec-1] o.i.dao.UserMapper.selectUserByName      : ==>  Preparing: SELECT * from user where userName = ?
2018-05-17 11:02:05.342 DEBUG 7380 --- [nio-1024-exec-1] o.i.dao.UserMapper.selectUserByName      : ==> Parameters: ricky(String)
2018-05-17 11:02:05.365 DEBUG 7380 --- [nio-1024-exec-1] o.i.dao.UserMapper.selectUserByName      : <==      Total: 1
2018-05-17 11:02:05.366 DEBUG 7380 --- [nio-1024-exec-1] org.mybatis.spring.SqlSessionUtils       : Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6c7ef00b]
2018-05-17 11:02:05.366 DEBUG 7380 --- [nio-1024-exec-1] org.mybatis.spring.SqlSessionUtils       : Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6c7ef00b]
2018-05-17 11:02:05.366 DEBUG 7380 --- [nio-1024-exec-1] org.mybatis.spring.SqlSessionUtils       : Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6c7ef00b]
2018-05-17 11:02:05.366 DEBUG 7380 --- [nio-1024-exec-1] org.mybatis.spring.SqlSessionUtils       : Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6c7ef00b]

有其他的好方法吗?

@Smith-Cruise
Copy link
Owner

额 我也没想出来,就删除了吧,老实说那个DefaultAdvisorAutoProxyCreator配置意义不大

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants