-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
58 changed files
with
907 additions
and
633 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 0 additions & 37 deletions
37
codes/data/jdbc/basics/src/main/java/example/spring/data/jdbc/TransactionDemo.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 38 additions & 6 deletions
44
codes/data/jdbc/basics/src/main/java/example/spring/data/jdbc/UserDaoTxServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,64 @@ | ||
package example.spring.data.jdbc; | ||
|
||
import cn.hutool.json.JSONUtil; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.TransactionStatus; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.transaction.support.TransactionCallbackWithoutResult; | ||
import org.springframework.transaction.support.TransactionTemplate; | ||
|
||
/** | ||
* 事务服务 | ||
* | ||
* @author <a href="mailto:[email protected]">Zhang Peng</a> | ||
* @since 2023-01-22 | ||
*/ | ||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class UserDaoTxServiceImpl implements UserDaoTxService { | ||
|
||
private final UserDao userDao; | ||
private final JdbcTemplate jdbcTemplate; | ||
private final TransactionTemplate transactionTemplate; | ||
|
||
@Override | ||
public void noTransaction() { | ||
userDao.insert(new User("王五", 18, "深圳", "[email protected]")); | ||
public void noTransaction(User entity) { | ||
userDao.insert(entity); | ||
log.info("插入一条记录: {}", JSONUtil.toJsonStr(entity)); | ||
throw new RuntimeException("强制异常"); | ||
} | ||
|
||
@Override | ||
@Transactional(rollbackFor = Exception.class) | ||
public void withTransaction() { | ||
userDao.insert(new User("赵六", 18, "深圳", "[email protected]")); | ||
throw new RuntimeException("强制异常"); | ||
@Transactional(rollbackFor = RollbackException.class) | ||
public void withTransaction(User entity) { | ||
userDao.insert(entity); | ||
log.info("插入一条记录: {}", JSONUtil.toJsonStr(entity)); | ||
throw new RollbackException("强制异常"); | ||
} | ||
|
||
@Override | ||
public void withTransaction2(User entity) { | ||
transactionTemplate.execute(new TransactionCallbackWithoutResult() { | ||
@Override | ||
protected void doInTransactionWithoutResult(TransactionStatus status) { | ||
jdbcTemplate.update("INSERT INTO `t_user`(`name`, `age`, `address`, `email`) VALUES(?, ?, ?, ?)", | ||
entity.getName(), entity.getAge(), entity.getAddress(), entity.getEmail()); | ||
log.info("写入一条记录:{}", entity); | ||
status.setRollbackOnly(); | ||
} | ||
}); | ||
} | ||
|
||
static class RollbackException extends RuntimeException { | ||
|
||
public RollbackException(String message) { | ||
super(message); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
-- https://dunwu.github.io/db-tutorial/#/sql/mysql/README | ||
-- ------------------------------------------------------------------- | ||
|
||
INSERT INTO `user` (`name`, `age`, `address`, `email`) | ||
INSERT INTO `t_user` (`name`, `age`, `address`, `email`) | ||
VALUES ('张三', 18, '北京', '[email protected]'); | ||
INSERT INTO `user` (`name`, `age`, `address`, `email`) | ||
INSERT INTO `t_user` (`name`, `age`, `address`, `email`) | ||
VALUES ('李四', 19, '上海', '[email protected]'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
@@ -16,6 +17,7 @@ | |
*/ | ||
@Slf4j | ||
@Rollback | ||
@DisplayName("Spring 事务示例") | ||
@SpringBootTest(classes = { DataJdbcApplication.class }) | ||
public class DataTxTests { | ||
|
||
|
@@ -25,34 +27,51 @@ public class DataTxTests { | |
@Autowired | ||
private UserDaoTxService userDaoTxService; | ||
|
||
@BeforeEach | ||
public void before() { | ||
userDao.truncate(); | ||
} | ||
|
||
@Test | ||
@DisplayName("没有事务的例子") | ||
@DisplayName("无事务示例") | ||
public void noTransaction() { | ||
Integer oldNum = userDao.count(); | ||
|
||
User entity = new User("王五", 18, "深圳", "[email protected]"); | ||
try { | ||
userDaoTxService.noTransaction(); | ||
userDaoTxService.noTransaction(entity); | ||
} catch (Exception e) { | ||
// do nothing | ||
} | ||
|
||
Integer newNum = userDao.count(); | ||
Assertions.assertNotEquals(oldNum, newNum); | ||
User record = userDao.queryByName("王五"); | ||
Assertions.assertNotNull(record); | ||
} | ||
|
||
@Test | ||
@DisplayName("有事务的例子") | ||
@DisplayName("声明式事务示例") | ||
public void withTransaction() { | ||
Integer oldNum = userDao.count(); | ||
User entity = new User("赵六", 18, "深圳", "[email protected]"); | ||
try { | ||
userDaoTxService.withTransaction(entity); | ||
} catch (Exception e) { | ||
// do nothing | ||
} | ||
|
||
User record = userDao.queryByName("赵六"); | ||
Assertions.assertNull(record); | ||
} | ||
|
||
@Test | ||
@DisplayName("编程式事务示例") | ||
public void withTransaction2() { | ||
User entity = new User("钱七", 20, "南京", "[email protected]"); | ||
try { | ||
userDaoTxService.withTransaction(); | ||
userDaoTxService.withTransaction2(entity); | ||
} catch (Exception e) { | ||
// do nothing | ||
} | ||
|
||
Integer newNum = userDao.count(); | ||
Assertions.assertEquals(oldNum, newNum); | ||
User record = userDao.queryByName("钱七"); | ||
Assertions.assertNull(record); | ||
} | ||
|
||
} |
Oops, something went wrong.
898998