Ziemowit Stolarczyk
Ziemowit Stolarczyk

Reputation: 1039

Stateless session in Spring Boot

We have some REST calls which performs quite massive operations on data (but not via Spring-Data). Because of that we would like to have Stateless Session for them.

Question is how to make it properly in Spring Boot with Hibernate and JPA? Because when I make simple test where I call some repository :

@Repository 
public class HelloRepository {

  @PersistenceContext   
  private EntityManager entityManager;

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

}

it gives me always true. I have feeling that session there should not be opened as I want to use Stateless Session.

Controller and Service do not do anything special. No @Transactional annotations etc:

@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();
  }
}

So question is: HOW to inform my app "please do not open session there I want to use stateless session"?

Upvotes: 1

Views: 2751

Answers (1)

Stephane Nicoll
Stephane Nicoll

Reputation: 33101

Add the following to application.properties

spring.jpa.open-in-view=false

We're considering switching that to false by default in Spring Boot 2.0

Upvotes: 2

Related Questions