当前位置: 首页 > 知识库问答 >
问题:

连接池错误(Spring、hibernate)。怎么解决这个?

沈伟
2023-03-14
WARNING: com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@3531a5ff -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30). Last acquisition attempt exception: 

2020年4月19日11:14:30 PM com.mchange.v2.resourcepool.basicresourcepool警告:由于无法获取资源,com.mchange.v2.resourcepool.basicresourcepool@7439cf4b正在中断所有等待资源签出的线程。将重试以响应新的客户端请求。2020年04月19日11:14:30 PM org.hibernate.dialt.方言信息:hhh000400:使用方言:org.hibernate.dialt.mysqldialector

这是我的密码

@Autowired
private Environment env;

private Logger logger = Logger.getLogger(getClass().getName());

// define a bean for ViewResolver

@Bean
public DataSource myDataSource() {

    // create connection pool
    ComboPooledDataSource myDataSource = new ComboPooledDataSource();

    // set the jdbc driver
    try {
        myDataSource.setDriverClass("com.mysql.jdbc.Driver");       
    }
    catch (PropertyVetoException exc) {
        throw new RuntimeException(exc);
    }

    // for sanity's sake, let's log url and user ... just to make sure we are reading the data
    logger.info("jdbc.url=" + env.getProperty("jdbc.url"));
    logger.info("jdbc.user=" + env.getProperty("jdbc.user"));

    // set database connection props
    myDataSource.setJdbcUrl(env.getProperty("jdbc.url"));
    myDataSource.setUser(env.getProperty("jdbc.user"));
    myDataSource.setPassword(env.getProperty("jdbc.password"));

    // set connection pool props
    myDataSource.setInitialPoolSize(getIntProperty("connection.pool.initialPoolSize"));
    myDataSource.setMinPoolSize(getIntProperty("connection.pool.minPoolSize"));
    myDataSource.setMaxPoolSize(getIntProperty("connection.pool.maxPoolSize"));     
    myDataSource.setMaxIdleTime(getIntProperty("connection.pool.maxIdleTime"));

    return myDataSource;
}

private Properties getHibernateProperties() {

    // set hibernate properties
    Properties props = new Properties();

    props.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
    props.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql"));

    return props;               
}


// need a helper method 
// read environment property and convert to int

private int getIntProperty(String propName) {

    String propVal = env.getProperty(propName);

    // now convert to int
    int intPropVal = Integer.parseInt(propVal);

    return intPropVal;
}   

@Bean
public LocalSessionFactoryBean sessionFactory(){

    // create session factories
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();

    // set the properties
    sessionFactory.setDataSource(myDataSource());
    sessionFactory.setPackagesToScan(env.getProperty("hibernate.packagesToScan"));
    sessionFactory.setHibernateProperties(getHibernateProperties());

    return sessionFactory;
}

@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {

    // setup transaction manager based on session factory
    HibernateTransactionManager txManager = new HibernateTransactionManager();
    txManager.setSessionFactory(sessionFactory);

    return txManager;
}   

}

我该怎么解决这个?

共有1个答案

习淇
2023-03-14

您的问题是您没有到池中的DB的免费连接。我不知道它在冬眠状态下是怎么工作的。但是你应该注意你的数据库访问。使用后应关闭连接。在JDBC中,只需使用“try with resources”(try(){...})打开它,或者使用“finally”块关闭连接。

 类似资料:
  • 问题: impl: @Service public class GraduateCollegeServiceImpl extends ServiceImpl<GraduateCollegeMapper, GraduateCollege> implements IGraduateCollegeService { controller: @Api(tags="各高校") @RestController

  • 问题内容: 我似乎无法hibernate使用c3p0进行连接池,它说 hibernate配置: 问题答案:

  • 以下代码: hibernate.cfg.xml: hibernateutil.java: 为了获得最好的帮助,当我尝试连接this Web服务时,请在Android Studio中查看my应用程序的日志: org.hibernate.exception.internal.standardsqlexceptionconverter.convert(standardsqlexceptionconver

  • 失败:生成失败,出现异常。 > 其中:Script“C:\flutter\packages\flutter_tools\gradle\flutter.gradle”行:900 错误:任务“:app:CompileFlutterBuildDebug”执行失败。 进程“command”C:\flutter\bin\flutter.bat“已完成,退出值为非零%1 生成在%12s中失败异常:Gradle

  • 在Spring boot application.properties文件中,我们有以下选项: 这是我的存储库类 这是服务类 问题是,userRepository如何创建到DB的连接,以及它是否会使用我的应用程序属性文件中的连接池。我来自JDBC和hibernate,在那里我使用DataManager、DataSource、Connection类来使用连接池,但是在spring boot中,我没有

  • 说我这个属性无效、不合法的,没有定义的 controller是有定义对象的,结果在controller就报上面的错了 InspectionPlan 对象有个属性是private List<FEntity> FEntity; FEntity对象属性FInspectionStandard的get/set方法也有,名字也取跟前端一样。但提交就报错了。请问这是怎么回事? js请求是这样的