弹性魔豆 - >使用Grails RDS连接错误 [英] Elastic Beanstalk -> RDS connection error using Grails

查看:249
本文介绍了弹性魔豆 - >使用Grails RDS连接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我部署一个Grails应用程序到亚马逊网络服务弹性魔豆和利用RDS。我使用的Grails,弹簧安全(RDS表)。该应用程序似乎做工精细(登录和数据RDS被拉回来)。但是,不时我第一次登录我得到一个数据库连接错误。第二次,(马上)工作正常。我略有不同做的唯一的事情是试图使用Java系统特性的连接字符串,用户名和密码来外部连接属性:

I'm deploying a Grails application to Amazon Web Services Elastic Beanstalk and leveraging RDS. I'm using Grails, Spring-Security (RDS tables). The application appears to work fine (login and data in RDS being pulled back). However, from time to time the first time I login I get a database connection error. The second time it (immediately) works fine. The only thing I'm doing slightly different is trying to use Java system properties for the connection string, username and password to externalize the connection properties:

production {
            dataSource {
            url = System.getProperty("JDBC_CONNECTION_STRING")
            driverClassName = "com.mysql.jdbc.Driver"
            dbCreate = "validate"
            dialect = org.hibernate.dialect.MySQL5InnoDBDialect
            username = System.getProperty("PARAM1")
            password = System.getProperty("PARAM2")
        }
    }

堆栈跟踪:

Caused by: org.hibernate.TransactionException: JDBC begin failed: 
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:96)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1353)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:555)
... 80 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 41,541,715 milliseconds ago.  The last packet sent successfully to the server was 41,541,716 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:407)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
    at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3358)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1970)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2150)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2620)
    at com.mysql.jdbc.ConnectionImpl.setAutoCommit(ConnectionImpl.java:5022)
    at org.apache.commons.dbcp.DelegatingConnection.setAutoCommit(DelegatingConnection.java:371)
    at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.setAutoCommit(PoolingDataSource.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy$TransactionAwareInvocationHandler.invoke(TransactionAwareDataSourceProxy.java:239)
    at $Proxy11.setAutoCommit(Unknown Source)
    at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:91)
    ... 82 more
Caused by: java.net.SocketException: Connection timed out
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
    at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
    at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
    at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3339)
    ... 95 more

我已经试过什么: 我附?autoReconnect的=真到了JDBC_CONNECTION_STRING

What I've tried: I appended ?autoReconnect=true to the JDBC_CONNECTION_STRING

我把这个添加到conf /春/ resources.groovy:

I've added this to conf/spring/resources.groovy:

beans = {       
    dataSource(BasicDataSource) {

        minEvictableIdleTimeMillis=1800000
        timeBetweenEvictionRunsMillis=1800000
        numTestsPerEvictionRun=3
        testOnBorrow=true
        testWhileIdle=true
        testOnReturn=true
        validationQuery="SELECT 1"
    }

}

不过是在正确的地方,也可以/应被添加到每个ENV配置DataSource.groovy中的文件?此外,上述唯一似乎是有效的,如果我的URL添加/ driverClassName,用户名和密码,现在意味着是在多个地方。如果解决了这个问题,我还没有得到证实,但有没有办法让这一切都在每ENV一个地方?

However is that the right place or can/should it be added to the datasource.groovy file per env configuration? Also, the above only seems to be valid if I add the URL/driverClassName, username, and password which now means that is in multiple places. I haven't confirmed if it solves the problem, but is there a way to have all this in one place per env?

推荐答案

这应该工作:

production {
        dataSource {
            url = System.getProperty("JDBC_CONNECTION_STRING")
            driverClassName = "com.mysql.jdbc.Driver"
            dbCreate = "validate"
            dialect = org.hibernate.dialect.MySQL5InnoDBDialect
            username = System.getProperty("PARAM1")
            password = System.getProperty("PARAM2")

            //configure DBCP
            properties {
                minEvictableIdleTimeMillis=1800000
                timeBetweenEvictionRunsMillis=1800000
                numTestsPerEvictionRun=3
                testOnBorrow=true
                testWhileIdle=true
                testOnReturn=true

                validationQuery="SELECT 1"
            }
        }
}

这篇关于弹性魔豆 - >使用Grails RDS连接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆