Commit 8d872b71 authored by 楊慶堂's avatar 楊慶堂

測試 Transactional

parent 59fee741
......@@ -29,10 +29,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
......
package org.ylhealth.ym.springtest.controller;
import javax.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.ylhealth.ym.springtest.entity.UserInfo;
import org.ylhealth.ym.springtest.service.TransactionTestService;
@RestController
public class TestController {
@Inject private TransactionTestService userInfoService;
private Logger logger = LoggerFactory.getLogger(getClass());
@GetMapping("/Test1")
public UserInfo test1() {
return userInfoService.findUserInfo("aaa1");
}
@GetMapping("/create")
public UserInfo create() {
UserInfo u = new UserInfo("aaa1", "小虫", "闲谈软体架构:休息时间");
userInfoService.save(u);
return u;
}
@GetMapping("/testTA")
public String testTransaction() {
userInfoService.testTransaction();
return "結束";
}
@GetMapping("/testTA1")
public String testTransaction3() {
userInfoService.testTransaction3();
return "結束2";
}
@GetMapping("/testTA2")
public String testTransaction2() {
logger.debug("testTransaction2 start");
userInfoService.testTransaction1();
userInfoService.testTransaction2();
logger.debug("testTransaction2 end");
return "結束3";
}
}
package org.ylhealth.ym.springtest.entity;
// Generated 2018/9/3 下午 12:25:54 by Hibernate Tools 4.3.2-SNAPSHOT
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.Nationalized;
/** UserInfo generated by hbm2java */
@Entity
@Table(name = "UserInfo")
public class UserInfo implements java.io.Serializable {
private String id;
private String name;
private String cname;
public UserInfo() {}
public UserInfo(String id) {
this.id = id;
}
public UserInfo(String id, String name, String cname) {
this.id = id;
this.name = name;
this.cname = cname;
}
@Id
@Column(name = "id", unique = true, nullable = false, length = 36)
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
@Column(name = "Name", length = 50)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Nationalized
@Column(name = "CName")
public String getCname() {
return this.cname;
}
public void setCname(String cname) {
this.cname = cname;
}
}
package org.ylhealth.ym.springtest.repo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.ylhealth.ym.springtest.entity.UserInfo;
public interface UserInfoRepo extends JpaRepository<UserInfo, String> {}
package org.ylhealth.ym.springtest.service;
import org.ylhealth.ym.springtest.entity.UserInfo;
public interface TransactionTestService {
public UserInfo findUserInfo(String id);
public void save(UserInfo u);
public void testTransaction();
public void testTransaction1();
public void testTransaction2();
public void testTransaction3();
}
package org.ylhealth.ym.springtest.service;
import javax.inject.Inject;
import javax.transaction.Transactional;
import javax.transaction.Transactional.TxType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.ylhealth.ym.springtest.entity.UserInfo;
import org.ylhealth.ym.springtest.repo.UserInfoRepo;
/**
* 測試 @Transactional 的使用
* 在 servcie 中互 call,被 call 的 method , annotation 是無效的。
* 觀察 o.s.orm.jpa.JpaTransactionManager 的 log 是不是在 method 中的 log 之前還是之後來辨断
*/
@Service
public class TransactionTestServiceImpl implements TransactionTestService {
private Logger logger = LoggerFactory.getLogger(getClass());
@Inject private UserInfoRepo userInfoRepo;
@Override
public UserInfo findUserInfo(String id) {
return userInfoRepo.findOne(id);
}
@Override
public void save(UserInfo u) {
userInfoRepo.save(u);
}
@Override
@Transactional(value=TxType.REQUIRES_NEW)
public void testTransaction1() {
logger.debug("testTransaction1 start");
userInfoRepo.findOne("aaa");
logger.debug("testTransaction1 end");
}
@Override
@Transactional(value=TxType.REQUIRES_NEW)
public void testTransaction2() {
logger.debug("testTransaction2 start");
userInfoRepo.findOne("aaa1");
logger.debug("testTransaction2 end");
}
/**
* service 中互 call, 被 call 的 method annotation 是無效的
* transaction 會在實際使用到時才產生
*/
@Override
public void testTransaction() {
logger.debug("testTransaction start");
testTransaction1();
testTransaction2();
logger.debug("testTransaction end");
}
/**
* 只會產生一個 Transaction
*/
@Override
@Transactional(value=TxType.REQUIRED)
public void testTransaction3() {
logger.debug("testTransaction3 start");
testTransaction1();
testTransaction2();
logger.debug("testTransaction3 end");
}
}
spring:
devtools:
restart:
enabled: true
livereload:
enabled: true
datasource:
url: jdbc:sqlserver://192.168.2.11:1433;databaseName=CRM_DEMO;sendStringParametersAsUnicode=false;
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
username: archuser
password: archuser
max-wait: 10000
max-active: 10
test-on-borrow: true
validation-query: SELECT 1
jmx-enabled: true
\ No newline at end of file
devtools:
restart:
enabled: true
livereload:
enabled: true
datasource:
url: jdbc:sqlserver://10.32.86.125:443;databaseName=TestDB;sendStringParametersAsUnicode=false;
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
username: archuser
password: archuser
jmx-enabled: true
jpa:
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
properties:
hibernate:
show_sql: true
format_sql: false
dialect: org.hibernate.dialect.SQLServer2012Dialect
globally_quoted_identifiers: true
temp:
use_jdbc_metadata_defaults: false
logging:
level:
org:
ylhealth.ym.springtest: DEBUG
springframework:
orm.jpa.JpaTransactionManager: DEBUG # 設為 DEBUG 可查看 transaction
transaction: DEBUG # 設為 DEBUG 可查看 transaction
web.filter.CommonsRequestLoggingFilter: DEBUG
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment