在我的spring boot应用程序中,我有一个带有100个固定线程的ThreadPoolExecutor。我已将数据源配置为:
@Bean
@Primary
public LocalContainerEntityManagerFactoryBean entityManager() throws IOException {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dbDatasource());
em.setPackagesToScan(new String[]{"com.pkg"});
em.setPersistenceUnitName("dbEntityManager");
HibernatePersistenceProvider hibernatePersistenceProvider = new HibernatePersistenceProvider();
em.setPersistenceProvider( hibernatePersistenceProvider );
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
properties.put("hibernate.naming-strategy", env.getProperty("spring.jpa.hibernate.naming-strategy"));
properties.put("hibernate.show-sql", env.getProperty("jdbc.show-sql"));
properties.put("logging.level.org.hibernate.SQL", env.getProperty("logging.level.org.hibernate.SQL"));
properties.put("logging.level.org.hibernate.type", env.getProperty("logging.level.org.hibernate.type"));
em.setJpaPropertyMap(properties);
return em;
}
@Primary
@Bean
public DataSource dbDatasource() throws IOException {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("spring.datasource.driverClassName"));
dataSource.setUrl(env.getProperty("spring.datasource.url"));
dataSource.setUsername(env.getProperty("spring.datasource.username"));
dataSource.setPassword("");
System.out.println("DATABASE:" + dataSource.getUrl() +" "+ dataSource.getUsername());
return dataSource;
}
@Primary
@Bean
public PlatformTransactionManager transactionManager() throws IOException{
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManager().getObject());
return transactionManager;
}
JpaRepository的repository.save(entity)是线程中发生的操作之一。第一次保存大约需要222毫秒,但是由于有负载,我看到每个请求保存的时间增加了50-100毫秒,每次保存最多可以达到10秒。
此外,我的CPU使用率猛增到90%。
有人可以帮助我了解我所缺少的吗?