问题
Using Spring boot 1.3.1
I don't understand why @RestController are Transactionnal by default. I haven't found anything saying so in the docs.
Example which pushes this fact that the method findOne() in the controller below is Transactionnal:
@RestController
@RequestMapping("/books")
public class BookController {
@RequestMapping("/{id}")
public Book findOne(@PathVariable Long id) {
Book book = this.bookDao.findOneBookById(id);
// following line
// => triggers a select author0_.id as id1_0_0_ etc... // where author0_.id=?
System.out.println(book.getAuthor().getFirstname());
return book;
}
}
The line with the System.out.println(book.getAuthor().getFirstname()); should raise a LazyInitiaizationFailure BUT here it is successful and trigger the select of an an Author. So the method findOne seems to be transactionnal. With the eclipse debugger I can be sure that it is really this line that triggers the complementary select. But Why is that method transactionnal ?
@Configuration
@ComponentScan(basePackageClasses = _Controller.class)
@Import(BusinessConfig.class)
public class WebConfig extends WebMvcConfigurerAdapter {
// ... here the conf to setup Jackson Hibernate4Module
}
@Configuration
@EnableAutoConfiguration
@EnableTransactionManagement
@EntityScan(basePackageClasses = _Model.class)
@ComponentScan(basePackageClasses = { _Dao.class })
public class BusinessConfig {
}
@SpringBootApplication
public class BookstoreStartForWebEmbedded {
public static void main(String[] args) {
SpringApplication.run(BookstoreStartForWebEmbedded.class, args);
}
}
libs :
spring-boot-starter 1.3.1.RELEASE
spring-boot-starter-test : 1.3.1.RELEASE
spring-boot-starter-valisation : 1.3.1.RELEASE
spring-boot-starter-web : 1.3.1.RELEASE
spring-boot-starter-data-jpa : 1.3.1.RELEASE
postgresql: 9.4-1206-jdbc41
querydsl-jps:3.7.0
jackson-annotations:2.6.4
jackson-datatype-hibernate4:2.6.4
any idea ?
If it is a feature, i would like to switch it off...
回答1:
In addition to MirMasej answers, there is one more thing: Spring Boot will automatically register an OpenEntityManagerInViewInterceptor
when the following conditions are true:
- you have a web application
- you use JPA
Both conditions are true in your case. This interceptor holds the entity manager open for the whole duration of a request. The auto configuration occurs in the class JpaBaseConfiguration
.
If you don't want that behaviour, you can add the following property to your application.properties file:
spring.jpa.open-in-view=false
Btw. this behaviour is completely independent of transactions, it's only related to the lifecycle of the entity manager. You can still have two separate transactions and no LazyInitializationException, if both transactions have the same underlying entity manager instance.
回答2:
One-to-one relations are always eagerly fetched. Judging by method names book.getAuthor().getFirstname()
, book->author and author->firstName are such relations.
LazyInitializationException
will only occur for lazy collections.
来源:https://stackoverflow.com/questions/34641670/restcontroller-methods-seem-to-be-transactional-by-default-why