<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.wix</groupId>
<artifactId>wix-embedded-mysql</artifactId>
<version>4.6.1</version>
<scope>test</scope>
</dependency>
디비가 동작하면서 데이터가 내려가기때문에 deleteAll 할 필요 없음
직접 환경의 접속정보를 넣지 않았음
public void transactionTest(Customer customer) {
String updateNameSql = "UPDATE customers SET name = ? WHERE customer_id = UUID_TO_BIN(?)";
String updateEmailSql = "UPDATE customers SET email = ? WHERE customer_id = UUID_TO_BIN(?)";
Connection connection = null; //RollBack 해주기 위해 Try문 밖으로 뻄
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost/order_mgmt", "root", "test1234");
connection.setAutoCommit(false); // 없으면 커밋이 되기때문에 롤백 안댐
try (
var updateNameStatement = connection.prepareStatement(updateNameSql);
var updateEmailStatement = connection.prepareStatement(updateEmailSql);
) {
updateNameStatement.setString(1, customer.getName());
updateNameStatement.setBytes(2, customer.getCustomerId().toString().getBytes());
updateNameStatement.execute();
updateEmailStatement.setString(1, customer.getEmail());
updateEmailStatement.setBytes(2, customer.getCustomerId().toString().getBytes());
updateEmailStatement.execute();
connection.setAutoCommit(true);
}
}catch (SQLException exception) {
if (connection != null) {
try {
connection.rollback();
connection.close();
} catch (SQLException throwable) {
logger.error("Got error while closing connection", throwable);
throw new RuntimeException(exception);
}
}
logger.error("Got error while closing connection", exception);
throw new RuntimeException(exception);
}
}
public static void main(String[] args) {
var customerRepository = new JdbcCustomerRepository();
//이메일에 유니크가 깨지기 때문에 에러 발생
customerRepository.transactionTest(new Customer(UUID.fromString("38b32b34-21dc-4e50-abd0-ae06ffa5c126"), "updated-user", "new-user2@gmail.com", LocalDateTime.now()));
}