2016-12-01 38 views
1

我們有一些REST調用對數據執行相當大規模的操作(但不是通過Spring-Data)。正因爲如此,我們希望爲他們制定無狀態會議。Spring Boot中的無狀態會話

問題是如何使用Hibernate和JPA在Spring Boot中正確使用? 因爲當我做簡單的測試,我調用一些庫:

@Repository 
public class HelloRepository { 

    @PersistenceContext 
    private EntityManager entityManager; 

    public boolean checkIfTransactionIsOpened() { 
    return entityManager.unwrap(Session.class).isOpen(); 
    } 

} 

它給了我總是如此。我感覺那個會議不應該打開,因爲我想使用無狀態會話。

控制器和服務沒有做任何特別的事情。沒有@Transactional註釋等:

@SpringBootApplication 
public class DemoApplication { 
    public static void main(String[] args) { 
    SpringApplication.run(DemoApplication.class, args); 
    } 
} 

@RestController 
public class HelloRest { 
    @Autowired 
    private HelloService helloService; 

    @GetMapping("/hello") 
    public ResponseEntity<Boolean> sayHello() { 
    return ResponseEntity.ok(helloService.checkIfTransactionIsOpened()); 
    } 
} 

@Service 
public class HelloService { 
    @Autowired 
    private HelloRepository helloRepository; 

    public boolean checkIfTransactionIsOpened() { 
    return helloRepository.checkIfTransactionIsOpened(); 
    } 
} 

所以問題是:如何通知我的應用程序「請不要開,盤中有我想要使用無狀態會話」?

回答