Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring启动应用程序-为每个线程创建HikariPool_Java_Spring Boot_Hikaricp - Fatal编程技术网

Java Spring启动应用程序-为每个线程创建HikariPool

Java Spring启动应用程序-为每个线程创建HikariPool,java,spring-boot,hikaricp,Java,Spring Boot,Hikaricp,我有一个2应用程序,它使用默认的Hikari连接池 我注意到每个线程都会创建一个新的连接池对象 启动时: 2019-11-13 10:52:26.693 INFO 79393 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... 2019-11-13 10:52:26.866 INFO 79393 --- [ main] com.zaxxer.

我有一个2应用程序,它使用默认的Hikari连接池

我注意到每个线程都会创建一个新的连接池对象

启动时:

2019-11-13 10:52:26.693  INFO 79393 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2019-11-13 10:52:26.866  INFO 79393 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
然后针对每个请求:

2019-11-13 10:53:24.165  INFO 79393 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariDataSource       : HikariPool-2 - Starting...
2019-11-13 10:53:24.169  INFO 79393 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariDataSource       : HikariPool-2 - Start completed.
2019-11-13 11:02:00.835  INFO 79393 --- [nio-8080-exec-5] com.zaxxer.hikari.HikariDataSource       : HikariPool-3 - Starting...
2019-11-13 11:02:00.839  INFO 79393 --- [nio-8080-exec-5] com.zaxxer.hikari.HikariDataSource       : HikariPool-3 - Start completed.
等等

这种行为是故意的还是我误解了什么

我使用的是
JdbcClientDetailsService
JdbcTokenStore
,因此每个请求都需要从
数据源获取信息

以下是身份验证服务器配置:

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {

    @Value("${spring.security.oauth2.resource.id}")
    private String resource_id;

    @Value("${spring.security.oauth2.resource.jwt.key-value}")
    private String signingKey;

    @Value("${spring.security.oauth2.client.client-id}")
    private String my_trusted_client_id;

    @Value("${spring.security.oauth2.client.client-secret}")
    private String secret;

    @Value("classpath:kcc.jks")
    private Resource keystore;

    @Value("${server.ssl.key-alias:keycrime}")
    private String keypair;

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private DataSource dataSource;

    @Autowired
    private UserService userService;

    private BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    @Autowired
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService)
            .passwordEncoder(passwordEncoder);
    }

    @Bean
    public JdbcTokenStore tokenStore() {
        return new JdbcTokenStore(dataSource);
    }

    @Bean
    public JwtAccessTokenConverter accessTokenConverter() {
        final JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(keystore, signingKey.toCharArray());
        converter.setKeyPair(keyStoreKeyFactory.getKeyPair(keypair));
        return converter;
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) {
        security.passwordEncoder(passwordEncoder)
            .checkTokenAccess("isAuthenticated()");
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

        clients.jdbc(dataSource).passwordEncoder(passwordEncoder)
                .withClient(my_trusted_client_id)
                .authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit")
                .authorities("ROLE_SYS_ADMIN", "ROLE_ADMIN", "ROLE_DBA")
                .scopes("read", "write", "trust")
                .resourceIds(resource_id)
                .accessTokenValiditySeconds(60 * 60 * 8)
                .refreshTokenValiditySeconds(60 * 60 * 24 * 365)
                .secret(secret);

    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) {

        endpoints.tokenStore(tokenStore())
                .accessTokenConverter(accessTokenConverter())
                .reuseRefreshTokens(false)
                .authenticationManager(authenticationManager);

    }

    @Bean
    @Primary
    public DefaultTokenServices tokenServices() {

        DefaultTokenServices defaultTokenServices = new DefaultTokenServices();

        defaultTokenServices.setTokenStore(tokenStore());
        defaultTokenServices.setSupportRefreshToken(true);
        defaultTokenServices.setReuseRefreshToken(false);

        return defaultTokenServices;
    }
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "...")
public class MySqlConfiguration {

    @Autowired
    private DataSource dataSource;

    @Primary
    @Bean(name = "entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManager() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setPersistenceUnitName("...");
        em.setPackagesToScan("...");
        em.setDataSource(dataSource);
        em.setJpaVendorAdapter(jpaVendorAdapter());
        return em;
    }

    @Primary
    @Bean(name = "transactionManager")
    public PlatformTransactionManager transactionManager() {
        JpaTransactionManager manager = new JpaTransactionManager();
        manager.setEntityManagerFactory(entityManager().getObject());
        return manager;
    }

    @Bean
    public JdbcTemplate jdbcTemplate () {
        return new JdbcTemplate(dataSource);
    }

    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
        return hibernateJpaVendorAdapter;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }

}
以下是MySQL配置:

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {

    @Value("${spring.security.oauth2.resource.id}")
    private String resource_id;

    @Value("${spring.security.oauth2.resource.jwt.key-value}")
    private String signingKey;

    @Value("${spring.security.oauth2.client.client-id}")
    private String my_trusted_client_id;

    @Value("${spring.security.oauth2.client.client-secret}")
    private String secret;

    @Value("classpath:kcc.jks")
    private Resource keystore;

    @Value("${server.ssl.key-alias:keycrime}")
    private String keypair;

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private DataSource dataSource;

    @Autowired
    private UserService userService;

    private BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    @Autowired
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService)
            .passwordEncoder(passwordEncoder);
    }

    @Bean
    public JdbcTokenStore tokenStore() {
        return new JdbcTokenStore(dataSource);
    }

    @Bean
    public JwtAccessTokenConverter accessTokenConverter() {
        final JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(keystore, signingKey.toCharArray());
        converter.setKeyPair(keyStoreKeyFactory.getKeyPair(keypair));
        return converter;
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) {
        security.passwordEncoder(passwordEncoder)
            .checkTokenAccess("isAuthenticated()");
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

        clients.jdbc(dataSource).passwordEncoder(passwordEncoder)
                .withClient(my_trusted_client_id)
                .authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit")
                .authorities("ROLE_SYS_ADMIN", "ROLE_ADMIN", "ROLE_DBA")
                .scopes("read", "write", "trust")
                .resourceIds(resource_id)
                .accessTokenValiditySeconds(60 * 60 * 8)
                .refreshTokenValiditySeconds(60 * 60 * 24 * 365)
                .secret(secret);

    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) {

        endpoints.tokenStore(tokenStore())
                .accessTokenConverter(accessTokenConverter())
                .reuseRefreshTokens(false)
                .authenticationManager(authenticationManager);

    }

    @Bean
    @Primary
    public DefaultTokenServices tokenServices() {

        DefaultTokenServices defaultTokenServices = new DefaultTokenServices();

        defaultTokenServices.setTokenStore(tokenStore());
        defaultTokenServices.setSupportRefreshToken(true);
        defaultTokenServices.setReuseRefreshToken(false);

        return defaultTokenServices;
    }
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "...")
public class MySqlConfiguration {

    @Autowired
    private DataSource dataSource;

    @Primary
    @Bean(name = "entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManager() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setPersistenceUnitName("...");
        em.setPackagesToScan("...");
        em.setDataSource(dataSource);
        em.setJpaVendorAdapter(jpaVendorAdapter());
        return em;
    }

    @Primary
    @Bean(name = "transactionManager")
    public PlatformTransactionManager transactionManager() {
        JpaTransactionManager manager = new JpaTransactionManager();
        manager.setEntityManagerFactory(entityManager().getObject());
        return manager;
    }

    @Bean
    public JdbcTemplate jdbcTemplate () {
        return new JdbcTemplate(dataSource);
    }

    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
        return hibernateJpaVendorAdapter;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }

}
编辑3

我发现在SpringBoot2中,配置SpringCloud时,Hikari数据源的默认作用域是refresh。我还在代码中发现了以下配置(我正在调查其他人编写的部分代码):


这就解释了这种行为。但是添加此配置可能是有原因的?

您配置错误。那么您是如何配置的呢?编辑添加授权服务器配置这很好,但由于问题在于连接池,在连接池端实际上没有太多配置,因为Hikari是Spring Boot 2的默认配置,并且数据源总是自动连接的。无论如何,我添加了我正在使用的MySql配置。但是默认配置不是“为每个线程创建一个连接池”,这甚至没有意义,所以有些配置错误。您可以禁用自动配置并自行配置,以查看是否有其他东西试图扰乱池。行为听起来像是bean的原型或请求范围,而这不应该仅仅发生。