Gau*_*eel 10 spring-boot spring-rest
使用Spring boot 1.3.1
我不明白为什么@RestController默认是Transactionnal.我在文档中没有发现任何这样的说法.
推动以下控制器中方法findOne()的事实是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;
}
}
Run Code Online (Sandbox Code Playgroud)
System.out.println的行(book.getAuthor().getFirstname()); 应该提出一个LazyInitiaizationFailure但这里它成功并触发一个作者的选择.所以方法findOne似乎是事务性的.使用eclipse调试器,我可以确定它确实是这一行触发了补充选择.但为什么那个方法是事务性的呢?
@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
Run Code Online (Sandbox Code Playgroud)
任何的想法 ?
如果它是一个功能,我想关闭它...
dun*_*nni 14
除了MirMasej的答案之外,还有一件事:Spring Boot会OpenEntityManagerInViewInterceptor在满足以下条件时自动注册:
在您的情况下,这两个条件都适用.此拦截器使实体管理器在整个请求期间保持打开状态.自动配置发生在类中JpaBaseConfiguration.
如果您不想要这种行为,可以将以下属性添加到application.properties文件中:
spring.jpa.open-in-view=false
Run Code Online (Sandbox Code Playgroud)
顺便说一句.此行为完全独立于事务,它仅与实体管理器的生命周期相关.如果两个事务都具有相同的基础实体管理器实例,您仍然可以有两个单独的事务而没有LazyInitializationException.
| 归档时间: |
|
| 查看次数: |
5248 次 |
| 最近记录: |