调用某些@Transactional方法后,C3P0筋疲力尽

时间:2019-05-09 14:52:39

标签: multi-tenant c3p0 hibernate-5.x

我已经实现了多租户架构,并且引入了C3P0连接池。

通过GUI,我尝试使用简单的搜索表单查询我的应用程序。该表单具有一个按钮,用于调用后端方法以提取我的结果。

如果单击几次搜索,由于C3P0连接池已用尽,我的应用程序冻结了。

因为C3P0的连接通过acquireIncrement参数管理,但到达时MAX_SIZE崩溃了

所以我的情况如下:

我在执行查询的地方有一个注释为@Service的类和一个注释为@Transactional的方法,如下所示:

@Override
@Transactional
public String findPatientCountDao(Paziente paziente, 
    boolean searchForAllPatients, boolean enabledDemetra) {
    String querySqlOrHql = this.createQuery(paziente, searchForAllPatients, true, 0, 0);

    Query q = getSession().createQuery(querySqlOrHql);
    List<Object[]> obj = q.list();
    return "" + obj.get(0);
}

我的Hibernate配置已由HibernateConf类管理,如下所示:

@Configuration
@EnableTransactionManagement
public class HibernateConf {

@Autowired
DataSourceBasedMultiTenantConnectionProviderImpl dataSources;
@Autowired
TenantSchemaResolver tenantSchemaResolver;


@Bean
public LocalSessionFactoryBean sessionFactory() {
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    if (TenantContext.getCurrentTenant()==null)
        TenantContext.setCurrentTenant("tenant_1");
    sessionFactory.setDataSource(dataSources.selectDataSource(TenantContext.getCurrentTenant()));
    sessionFactory.setPackagesToScan("it.spi");
    org.hibernate.cfg.Configuration configuration = new org.hibernate.cfg.Configuration();
    configuration.setProperties(hibernateProperties());

    //sessionFactory.setHibernateProperties(hibernateProperties());
    sessionFactory.setConfigLocation(new ClassPathResource("hibernate_ehr/hibernate.cfg.xml"));

    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
            .applySettings(configuration.getProperties()).build();
        System.out.println("Hibernate Java Config serviceRegistry created");
        sessionFactory = (LocalSessionFactoryBean) configuration.buildSessionFactory(serviceRegistry);

    return sessionFactory;
}

@Bean
public HibernateTransactionManager transactionManager() {
    HibernateTransactionManager transactionManager =
            new HibernateTransactionManager();
    transactionManager.setSessionFactory(sessionFactory().getObject());
    return transactionManager;
}


private final Properties hibernateProperties() {
    Properties hibernateProperties = new Properties();
    hibernateProperties.put(Environment.MULTI_TENANT, MultiTenancyStrategy.DATABASE);
    hibernateProperties.put(Environment.MULTI_TENANT_CONNECTION_PROVIDER, dataSources);
    hibernateProperties.put(Environment.MULTI_TENANT_IDENTIFIER_RESOLVER, tenantSchemaResolver);
    hibernateProperties.put(Environment.USE_STREAMS_FOR_BINARY, true);
    hibernateProperties.put(Environment.DIALECT, "org.hibernate.dialect.Oracle10gDialect");
    hibernateProperties.put(Environment.DRIVER, "oracle.jdbc.OracleDriver");
    hibernateProperties.put(Environment.SHOW_SQL, false);
    hibernateProperties.put(Environment.FORMAT_SQL, false);
    hibernateProperties.put(Environment.USE_SQL_COMMENTS, false);
    hibernateProperties.put(Environment.C3P0_MIN_SIZE, 50);         //Minimum size of pool
    hibernateProperties.put(Environment.C3P0_MAX_SIZE, 100);        //Maximum size of pool
    hibernateProperties.put(Environment.C3P0_ACQUIRE_INCREMENT, 10);//Number of connections acquired at a time when pool is exhausted 
    hibernateProperties.put(Environment.C3P0_TIMEOUT, 1800);       //Connection idle time
    hibernateProperties.put(Environment.C3P0_MAX_STATEMENTS, 600); //PreparedStatement cache size
    return hibernateProperties;
}

我的数据源配置:

@Primary
@Bean(name = { "dataSource", "dataSource1" })
@ConfigurationProperties(prefix = "spring.multitenancy.datasource1")
public DataSource dataSource1() {
    DataSourceBuilder factory = DataSourceBuilder
            .create(this.multitenancyProperties.getDatasource1().getClassLoader())
            .driverClassName(this.multitenancyProperties.getDatasource1().getDriverClassName())
            .username(this.multitenancyProperties.getDatasource1().getUsername())
            .password(this.multitenancyProperties.getDatasource1().getPassword())
            .url(this.multitenancyProperties.getDatasource1().getUrl()).type(ComboPooledDataSource.class);
    return factory.build();
}

我的database.properties文件

spring.multitenancy.datasource.type = com.mchange.v2.c3p0.ComboPooledDataSource
spring.multitenancy.datasource.url=jdbc:oracle:thin:@xx.xx.xx.xx:1521/xxx
spring.multitenancy.datasource.username=xxx
spring.multitenancy.datasource.password=yyy
spring.multitenancy.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.multitenancy.datasource.testWhileIdle = true
spring.multitenancy.datasource.validationQuery = SELECT 1
spring.multitenancy.datasource.hibernate.c3p0.max_size=100
spring.multitenancy.datasource.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory

0 个答案:

没有答案