简体   繁体   English

Spring 启动 Rest + ClearDB mySQL “超过'max_user_connections'”错误

[英]Spring Boot Rest + ClearDB mySQL “exceeded 'max_user_connections'” error

I keep randomly getting this error every once in a while: "java.sql.SQLSyntaxErrorException: User '{key}' has exceeded the 'max_user_connections' resource (current value: 10)".我每隔一段时间就会随机收到此错误:“java.sql.SQLSyntaxErrorException:用户'{key}'已超过'max_user_connections'资源(当前值:10)”。

I have tried googling help for this, but all I can find is:我试过谷歌搜索帮助,但我能找到的是:

  1. "increase the max connections limit" (which can't be done in free clearDB) “增加最大连接数限制”(不能在免费的 clearDB 中完成)
  2. "adjust maxActive amount" or "release old connections" (both of which I can't find how to do it in Spring Boot) “调整 maxActive 数量”或“释放旧连接”(我在 Spring Boot 中找不到如何操作)

Here's what my code looks like:这是我的代码的样子:

// application.properties
# Connect to heroku ClearDB MySql database
spring.datasource.url=jdbc:mysql://{heroku_url}?reconnect=true
spring.datasource.username={user}
spring.datasource.password={password}

# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto=update

#MySQL DIALECT
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.open-in-view=false
server.port=8080
@Configuration
public class DatabaseConfig {
    @Value("${spring.datasource.url}")
    private String dbUrl;

    @Bean
    public DataSource dataSource() {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(dbUrl);

        return new HikariDataSource(config);
    }
}

EDIT 1: I was following PauMAVA's instructions as best as I could and I came up with this code, which for some reason fails:编辑 1:我尽我所能遵循 PauMAVA 的说明,我想出了这段代码,但由于某种原因失败了:

@Configuration
public class DatabaseConfig {
    @Value("${spring.datasource.url}")
    private String dbUrl;

    public static DataSource ds;

    @Bean
    public DataSource dataSource() {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(dbUrl);

        DataSource ds = new HikariDataSource(config);
        DatabaseConfig.ds = ds;

        return ds;
    }
}
// Main class
public static void main(String[] args) {
    SpringApplication.run(BloggerApplication.class, args);

    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        public void run() {
            DataSource ds = DatabaseConfig.ds;
            if (ds != null) {
                try {
                    ds.getConnection().close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }, "Shutdown-thread"));
}

Whenever you create a connection object in you code, it is advisable to close the same in finally block.每当您在代码中创建连接 object 时,建议在 finally 块中关闭该连接。 This way the number of connections do not get exhausted.这样连接数就不会耗尽。

Hope this helps!希望这可以帮助!

You should close the DataSource on application termination so that no unused connections remain open.您应该在应用程序终止时关闭 DataSource,以便没有未使用的连接保持打开状态。

public void close(DataSource ds) {
    if(ds != null) {
        ds.close();
    }
}

But do this only on program termination as stated here .但仅在程序终止时执行操作,如此处所述。

To use the data source later (on closing) you can register the DataSource as a Field in your class:要稍后使用数据源(关闭时),您可以将数据源注册为 class 中的字段:

private DataSource ds;

@Bean
public DataSource dataSource() {
    HikariConfig config = new HikariConfig();
    config.setJdbcUrl(dbUrl);
    DataSource ds = new HikariDataSource(config);
    this.ds = ds;
    return ds;
}

If you are going to have more than one data source you can make a List based approach:如果您将拥有多个数据源,则可以采用基于列表的方法:

private List<DataSource> activeDataSources = new ArrayList<>();

public DataSource dataSource() {
    HikariConfig config = new HikariConfig();
    config.setJdbcUrl(dbUrl);
    DataSource ds = new HikariDataSource(config);
    this.activeDataSources.add(ds);
    return ds;
}

public void closeAllDataSources() {
    for(DataSource ds: this.activeDataSources) {
        if(ds != null) {
            ds.close();
        }
    }
    this.activeDataSources = new ArrayList<>();
}

To execute a function on program close refer to this .要在程序关闭时执行 function,请参阅

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySQL的max_user_connections错误 - max_user_connections error with mysql 在Heroku上出现“((用户已超出&#39;max_user_connections&#39;资源(当前值:10))”错误 - “(User has exceeded the 'max_user_connections' resource (current value: 10))” error on heroku Java和MySQL:超过“ max_user_connections”例外 - Java and MySQL: More than 'max_user_connections' exception 我如何解决 MySQL 中的“max_user_connections”问题? - How i can fix a 'max_user_connections' reached problem in MySQL? 在Hibernate中,用户root已具有超过&#39;max_user_connections&#39;个活动连接 - User root already has more than 'max_user_connections' active connections in Hibernate Spring Boot + ClearDB mySQL App deploy Heroku 问题 - Spring Boot + ClearDB mySQL App deploy Heroku problem 如何解决user1在Hibernate-HQL中已经具有超过“ max_user_connections”个活动连接? - How to resolve user1 already has more than 'max_user_connections' active connections in Hibernate- HQL? Spring Boot MySQL睡眠连接 - Spring boot mysql sleep connections Spring 启动 rest 模板通过 id 获取用户给出 RestClientException 错误 - Spring boot rest template get user by id gives RestClientException error spring 启动 Rest:NullPointerException 错误 - spring boot Rest : NullPointerException error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM