@Entity
public class User {
@Id
@GeneratedValue
private Long id;
private String username;
private String userdesc;
@Version
private Long version;
public User() {
}
public User(String username, String userdesc) {
this.username = username;
this.userdesc = userdesc;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserDesc() {
return userdesc;
}
public void setUserDesc(String userdesc) {
this.userdesc = userdesc;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
}
controller中通过sleep将线程沉睡,测试事务的提交性
@RestController
public class UserController {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
UserService userService;
@PostMapping("/changeone")
@Transactional
public String changeone() {
User user = userService.findUser("gang");
try {
logger.info("修改1 before:user--{}--Versdion:{}", user.getUserDesc(), user.getVersion());
Thread.sleep(25000);
user.setUserDesc("修改1");
logger.info("修改1 :user--{}--version:{}", user.getUserDesc(), user.getVersion());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
logger.info("eeeeeeeeeeeeee");
e.printStackTrace();
}
return "true";
}
@PostMapping("/changetwo")
@Transactional
public String changetwo() {
User user = userService.findUser("gang");
try {
logger.info("修改2 before:user--{}--version:{}", user.getUserDesc(), user.getVersion());
Thread.sleep(30000);
user.setUserDesc("修改2");
logger.info("修改2:user--{}--version:{}", user.getUserDesc(), user.getVersion());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
logger.info("eeeeeeeeeeeeee");
e.printStackTrace();
}
return "true";
}
@PostMapping("/changethree")
@Transactional
public String changethree() {
User user = userService.findUser("gang");
logger.info("修改3 before:user--{}--version:{}", user.getUserDesc(), user.getVersion());
user.setUserDesc("修改3");
logger.info("修改3 :user--{}--version:{}", user.getUserDesc(), user.getVersion());
return "true";
}
@PostMapping("/newuser")
@Transactional
public String newuser() {
logger.info("save user");
User user = new User();
user.setUserDesc("第一次创建");
user.setUsername("gang");
userService.saveUser(user);
return "true";
}
}
以及service及repository
@Service
public class UserService {
@Autowired
UserRepository userRepository;
public User findUser(String username){
return userRepository.findByUsername(username);
}
public void saveUser(User user){
userRepository.save(user);
}
}
UserRepository
public interface UserRepository extends JpaRepository<User,Long> {
User findByUsername(String username);
}
总结
使用很简单,version是自动增长的,唯一的缺点是抛出的异常不易捕获,捕获的方法:
@Resource
private UserTransaction rtc;
try {
rtc.begin();
User user = userService.findUser("gang");
user .setDesc("异常捕获");
rtc.commit();
} catch (OptimisticLockException e) {
throw new OptimisticLockException ();
} catch (Exception e) {
throw new Exception ();
}