docker实战(一):centos7 yum安装docker
docker实战(二):基础命令篇
docker实战(三):docker网络模式(超详细)
docker实战(四):docker架构原理
docker实战(五):docker镜像及仓库配置
docker实战(六):docker 网络及数据卷设置
docker实战(七):docker 性质及版本选择
认知升维: 道、法、术、器、势
Jpa与Druid线程池及Spring Boot整合(一)
Jpa与Druid线程池及Spring Boot整合(二):几个坑
附录官网文档:core.domain-events域事件
存储库管理的实体是聚合根。在领域驱动设计应用程序中,这些聚合根通常会发布领域事件。Spring Data 提供了一个名为 的注释@DomainEvents
,您可以在聚合根的方法上使用该注释,以使该发布尽可能简单,如以下示例所示:
class AnAggregateRoot {
@DomainEvents (1)
Collection
使用的方法@DomainEvents 可以返回单个事件实例或事件集合。它不能接受任何参数。 |
|
发布所有事件后,我们有一个用 注释的方法@AfterDomainEventPublication 。您可以使用它来潜在地清理要发布的事件列表(以及其他用途)。 |
每次调用以下 Spring 数据存储库方法之一时都会调用这些方法:
save(…)
,saveAll(…)
delete(…)
, deleteAll(…)
, deleteAllInBatch(…)
,deleteInBatch(…)
请注意,这些方法将聚合根实例作为参数。这就是为什么deleteById(…)
明显不存在的原因,因为实现可能会选择发出删除实例的查询,因此我们首先永远无法访问聚合实例。
在使用Spring Boot JPA时,open-in-view 选项默认为true。启动时在日志中会出现警告。
一坑:druid与jpa整合,启动出现如下警告:
JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
按照日志要求将 open-in-view=false。再次启动,如果在实体类中外键字段使用了懒加载模式,在视图层中调用数据时,则会出现 no session 异常。
解决方法:手动注册OpenEntityManagerInViewFilter过滤器,改变session的生命周期,当web请求关闭时才结束session。
javaConfig注入bean
@Bean public FilterRegistrationBean
registerOpenEntityManagerInViewFilterBean() { FilterRegistrationBean registrationBean = new FilterRegistrationBean<>(); OpenEntityManagerInViewFilter filter = new OpenEntityManagerInViewFilter(); registrationBean.setFilter(filter); registrationBean.setOrder(5); return registrationBean; } 解疑:
如果使用的是 JPA 则对应OpenEntityManagerInViewFilter,Hibernate对应OpenSessionInViewFilter
疑问:registrationBean.setOrder(5),order为什么是5
OpenSessionInViewInterceptor&OpenSessionInViewFilter,OpenEntityManagerInViewInterceptor&OpenEntityManagerInViewFilter这几个类的区别以及使的的场景
重新启动: 正常:
二坑: springboot整合JPA出现异常java.lang.IllegalArgumentException: Not a managed type: class xx
方案: 一
启动类或者JavaConfig添加添加扫描实体的路径: @EntityScan(basePackages = "org.jd.websocket.auth.data.reactor.jpa") @EnableTransactionManagement // 开启事务的支持 @EnableJpaRepositories(basePackages = {"org.jd.websocket.auth.data.reactor.repository","org.jd.websocket.auth.data.reactor.jpa"}, includeFilters = {@ComponentScan.Filter(type = FilterType.REGEX, pattern = ".*CrudRepository")}, excludeFilters = {@ComponentScan.Filter(type = FilterType.REGEX, pattern = ".*SomeOtherRepository")})
方案: 二
@Configuration javaConfig中注入Bean
@Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory() { HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); vendorAdapter.setGenerateDdl(true); LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); factory.setJpaVendorAdapter(vendorAdapter); // entity扫描的包路径 factory.setPackagesToScan("org.jd.websocket.auth.data.reactor.jpa"); factory.setDataSource(dataSource()); return factory; }
@Value("${spring.datasource.url}") private String dbUrl; @Value("${spring.datasource.username}") private String username; @Value("${spring.datasource.password}") private String password; @Value("${spring.datasource.driver-class-name}") private String driverClassName; @Value("${spring.datasource.initialSize}") private int initialSize; @Value("${spring.datasource.minIdle}") private int minIdle; @Value("${spring.datasource.maxActive}") private int maxActive; @Value("${spring.datasource.maxWait}") private int maxWait; @Value("${spring.datasource.timeBetweenEvictionRunsMillis}") private int timeBetweenEvictionRunsMillis; @Value("${spring.datasource.minEvictableIdleTimeMillis}") private int minEvictableIdleTimeMillis; @Value("${spring.datasource.validationQuery}") private String validationQuery; @Value("${spring.datasource.testWhileIdle}") private boolean testWhileIdle; @Value("${spring.datasource.testOnBorrow}") private boolean testOnBorrow; @Value("${spring.datasource.testOnReturn}") private boolean testOnReturn; @Value("${spring.datasource.poolPreparedStatements}") private boolean poolPreparedStatements; @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}") private int maxPoolPreparedStatementPerConnectionSize; @Value("${spring.datasource.filters}") private String filters; @Value("{spring.datasource.connectionProperties}") private String connectionProperties; @Bean @Primary public DataSource dataSource() { DruidDataSource datasource = new DruidDataSource(); datasource.setUrl(dbUrl); datasource.setUsername(username); datasource.setPassword(password); datasource.setDriverClassName(driverClassName); // configuration datasource.setInitialSize(initialSize); datasource.setMinIdle(minIdle); datasource.setMaxActive(maxActive); datasource.setMaxWait(maxWait); datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); datasource.setValidationQuery(validationQuery); datasource.setTestWhileIdle(testWhileIdle); datasource.setTestOnBorrow(testOnBorrow); datasource.setTestOnReturn(testOnReturn); datasource.setPoolPreparedStatements(poolPreparedStatements); datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize); try { datasource.setFilters(filters); } catch (SQLException e) { System.err.println("druid configuration initialization filter: " + e); } datasource.setConnectionProperties(connectionProperties); return datasource; } @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory() { HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); vendorAdapter.setGenerateDdl(true); LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); factory.setJpaVendorAdapter(vendorAdapter); // entity扫描的包路径 factory.setPackagesToScan("org.jd.websocket.auth.data.reactor.jpa"); factory.setDataSource(dataSource()); return factory; }
坑三: 多数据源注入,事务失效|解决方案;
* @Transactiona在多数据源下失效 * 在多数据源的情况下,如果transactionManager进行了分开配置,比如这里针对三个数据源配置了三个transactionManager * =========================================================================================================== * @Bean("formTransactionManager") * public PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder) { * return new JpaTransactionManager(Objects.requireNonNull(entityManagerFactory(builder).getObject())); * } *============================================================================================================= * @Bean("fileTransactionManager") * public PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder) { * return new JpaTransactionManager(Objects.requireNonNull(entityManagerFactory(builder).getObject())); * } * ============================================================================================================ * @Primary * @Bean("userTransactionManager") * public PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder) { * return new JpaTransactionManager(Objects.requireNonNull(entityManagerFactory(builder).getObject())); * } * ============================================================================================================= * * 那么在使用@Transactional的时候需要指定transactionManager * @Transactional("formTransactionManager")
@EnableTransactionManagement // 开启事务的支持
可以访问下druid监控相关数据:
http://localhost:9999/druid/
输入上面的账号和密码: 可以查看不同的选项卡,能帮助我们排查一些数据库相关问题