Grails mysql connection time out in production mode - grails

I have deployed a grails application in tomcat 7. Database connection works fine during normal time, but in the night the connection is getting closed. Next day I have to restart the tomcat to make it work.
Here is my connection string in the config file.
production {
dataSource {
dbCreate = "update"
pooled = true
logSql = true
driverClassName = "com.mysql.jdbc.Driver"
dialect = "org.hibernate.dialect.MySQL5InnoDBDialect"
url =
properties {
maxActive = -1
minEvictableIdleTimeMillis=1800000
timeBetweenEvictionRunsMillis=1800000
numTestsPerEvictionRun=3
testOnBorrow=true
testWhileIdle=true
testOnReturn=true
validationQuery="SELECT 1"
}
}
error log:
Broken pipe. Stacktrace follows:
java.net.SocketException: Broken pipe
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:3832)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2471)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2728)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2678)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1612)
also I see this message in the log file.
appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak.
How can I fix the connection time out issue.

You have to add autoReconnect=true to the connection url, it will help with such conection issues.
Like:
production {
dataSource {
...
url = 'jdbc:mysql://localhost/dbname?autoReconnect=true'
...
}
}

Related

Using H2 server mode for Grails integration tests

I want to try user H2 db in server mode for my grails integration tests, but there is a problem: H2GrailsPlugin: Started TCP Server - args: -tcp,-tcpPort,8043
ERROR pool.ConnectionPool - Unable to create initial connections of pool.
Message: Connection is broken: "java.net.ConnectException: Connection refused: localhost:8043"
Now I use grails h2 plugin, for running H2 server with next configuration in my Config.groovy:
plugins {
h2 {
tcpserver {tcpPort = 8043;}
}
}
This is my DataSourse:
test {
dataSource_one {
pooled = true
driverClassName = "org.h2.Driver"
username = "sa"
password = ""
dbCreate = "update"
//url = "jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
url = "jdbc:h2:tcp://localhost:8043/testDb"
}
Also I tried direct run of the Server in my Bootstrap.groovy:
import grails.util.Environment
import org.h2.tools.Server
class BootStrap {
Server server
def init = { servletContext ->
println "INIT"
if(Environment.current == Environment.TEST){
server = Server.createTcpServer('-tcpPort', '8043').start()
}
}
def destroy = {
server.stop()
}
}
but this code wasn't executed cause of exception was thrown before . It would be great, if someone help me with this issue.

Table "XXX" not found in Grails

Trying to import an old grails project, there seems to be a problem with the mappings. Everything loads perfectly until...
Server running. Browse to http://localhost:8080/prmptvServer
Configuring Spring Security Core ...
... finished configuring Spring Security Core
| Error 2015-11-30 09:06:43,636 [localhost-startStop-1] ERROR hbm2ddl.SchemaExport - HHH000389: Unsuccessful: alter table bank_account drop constraint FK_ss4uej5gx2a07srb540l15s21 if exists
| Error 2015-11-30 09:06:43,638 [localhost-startStop-1] ERROR hbm2ddl.SchemaExport - Table "BANK_ACCOUNT" not found; SQL statement: alter table bank_account drop constraint FK_ss4uej5gx2a07srb540l15s21 if exists [42102-176]
And the rest of domains can't load either. As far as I understand, if my DataSource contains the
dbCreate="create-drop"
, the DB should be rebuilt each time I restart the app, doesn't it? At least that's what I remember. So if it can't find the table, hasn't it been created? If it wasn't created when it should have been, shouldn't I get another error like "couldn't create table" ?
Bootstrap.groovy is all commented to do the debug easier.
DataSource.groovy
dataSource {
pooled = true
jmxExport = true
driverClassName = "org.h2.Driver"
username = "sa"
password = ""
}
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = false
// cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory' // Hibernate 3
cache.region.factory_class = 'org.hibernate.cache.ehcache.EhCacheRegionFactory' // Hibernate 4
singleSession = true // configure OSIV singleSession mode
flush.mode = 'manual' // OSIV session flush mode outside of transactional context
}
// environment specific settings
environments {
development {
dataSource {
dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', ''
url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
}
}...
BankAccount.groovy
class BankAccount {
SecUser user
String alias
String number
static constraints = {
alias nullable: false, blank: false
number nullable: false, blank: false
}
}
I believe this is not a critical problem. I get the same errors in my projects, but the app still working fine.
I believe there is some inconsistency in this configuration. Is not about you, but about grails.
Maybe this post answer your question -> here

Grails error: table or view does not exist

When ever I start my Grails application, I get these errors for all my domain classes.
ERROR hbm2ddl.SchemaExport - HHH000389: Unsuccessful: drop table domain_class cascade constraints
ERROR hbm2ddl.SchemaExport -ORA-01031: insufficient privileges
and then
ERROR hbm2ddl.SchemaExport -ORA-00942: table or view does not exist
I am using in-memory database for the application and my DataSource.groovy has this in it:
dataSource {
pooled = true
driverClassName = "org.h2.Driver"
username = "sa"
password = ""
}
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = false
cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
}
// environment specific settings
environments {
local {
dataSource {
dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', ''
url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
}
}
development {
dataSource {
dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
}
}
test {
dataSource {
dbCreate = "update"
url = "jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
}
}
Is there something wrong with the settings in DataSource?
I start my application with grails -Dgrails.env=local run-app -https
I tried to create objects during startup using BootStrap.groovy and even they fail. I use GGTS for development. What privileges is this talking about?
We found the answer. The dataSource in file instanceConfig.local.properties in the resources folder was pointing to an Oracle database and I am not configured to a role to create or drop tables there. Hence the insufficient privileges error.
Even though the DataSource.groovy had the in-memory database setting, I assume that the instanceConfig.local.properties overrides it. Thanks anyways for the help guys!

Defining an alternate connection pool in Grails 2.3.6

I know that, at some point between Grails 1.X and Grails 2.X, the default connection pooling library changed from commons-dbcp to tomcat-dbcp.
Now, I'm trying to configure either BoneCP or HikariCP as the connection pooling library for my Grails application.
However, I see that this answer offers a solution which might only apply to Grails 1.X.
I also found this Gist, but again, I don't know which Grails version it applies to.
So, is it possible to define a custom connection pool inside a Grails 2.3.6 application? Thanks!
UPDATE: OK so you actually need to tell Grails not to pool the datasources, since HikariCP is now taking care of this.
I saw connection weirdness in my apps if I left that switch on. So instead say:
pooled = false
OK yeah, #Joshua Moore is right.
I tried doing it with updated Grails methods and this is the relevant section of my resources.groovy file. As far as I can understand, the configuration values in Datasource.groovy are pulled into resources.groovy at runtime, after the target runtime environment has been identified (development, test or production).
def config = Holders.config
def dataSources = config.findAll {
it.key.toString().contains("dataSource_")
}
dataSources.each { key, value ->
def ds = value
"${key}"(HikariDataSource, { bean ->
def hp = new Properties()
hp.username = ds.username
hp.password = ds.password
hp.connectionTimeout = 6000
hp.maximumPoolSize = 60
hp.jdbcUrl = ds.url
hp.driverClassName = ds.driverClassName
HikariConfig hc = new HikariConfig(hp)
bean.constructorArgs = [hc]
})
}
And this is the relevant section of my DataSource.groovy configuration:
// environment specific settings
environments {
development {
dataSource_myapp1 {
pooled = false
username = "CONFIGURE_ME_EXTERNALLY"
password = "CONFIGURE_ME_EXTERNALLY"
driverClassName = 'oracle.jdbc.OracleDriver'
dialect = 'org.hibernate.dialect.Oracle10gDialect'
url = 'jdbc:oracle:thin:#MYDBHOST1:1521/MYSERVICEID1'
}
dataSource_myApp2 {
pooled = false
username = "CONFIGURE_ME_EXTERNALLY"
password = "CONFIGURE_ME_EXTERNALLY"
driverClassName = 'oracle.jdbc.OracleDriver'
dialect = 'org.hibernate.dialect.Oracle10gDialect'
url = 'jdbc:oracle:thin:#MYDBHOST2:1521/MYSERVICEID2'
}
}
}
In my case, it's pretty much the same for test and production environments. Thanks!

Grails Hibernate - how to keep data in hibernate cache still my grails application restarted

I created a new simple project.
I created a Book domain and mentioned cache=true in mapping.
I am setting some data by BootStrap.groovy configuration.
I added some print statements in BootStrap.groovy to check the data is added or not.
I put Hibernate second level cache is true.
I run application, it printed in console that "added this many records".
But the date not showing in my grid.
I tried with postgresSQL, with same data. My grid showing records. Just change in DataSource.groovy only.
Please help me, what I am missing
My Default hibernate configuration in DataSource.groovy
dataSource {
pooled = true
jmxExport = true
driverClassName = "org.h2.Driver"
username = "sa"
password = ""
}
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = false
cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory' // Hibernate 3
// cache.region.factory_class = 'org.hibernate.cache.ehcache.EhCacheRegionFactory' // Hibernate 4
singleSession = true // configure OSIV singleSession mode
}
In other words, how to keep data in hibernate cache still my grails application restarted.
**
**

Resources