Sunday, 21 July 2013

Grails - Broken Pipe and Cannot release connection

In one of my grails applications, I was getting the following exception often,

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:2734)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2322)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
at org.grails.datastore.gorm.GormStaticApi$_methodMissing_closure2.doCall(GormStaticApi.groovy:108)
at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:195)
at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
2013-07-21 12:28:16,794 [http-bio-80-exec-7] ERROR util.JDBCExceptionReporter  - Already closed.
2013-07-21 12:28:16,794 [http-bio-80-exec-7] ERROR util.JDBCExceptionReporter  - Already closed.
2013-07-21 12:28:16,807 [http-bio-80-exec-7] ERROR servlet.GrailsDispatcherServlet  - HandlerInterceptor.afterCompletion threw exception
org.hibernate.exception.GenericJDBCException: Cannot release connection
at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:195)
at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:195)
at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.sql.SQLException: Already closed.
at org.apache.commons.dbcp.PoolableConnection.close(PoolableConnection.java:114)
at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.close(PoolingDataSource.java:191)
... 7 more

The reason was the database connections in the pool was not used for some time and the connection got closed, and when the application tries to use that it was throwing the exception.

The solution worked to me is

Just added 3 properties into my DataSource.groovy file as follows,

dbCreate = "update" // one of 'create', 'create-drop','update'
url = "jdbc:mysql://localhost/mydb?useUnicode=yes&characterEncoding=UTF-8"
username = "root"
password = "pwd"
properties {
        validationQuery = "select 1"
        testWhileIdle = true
        testOnBorrow = true

 }

you can find more information on this properties at http://commons.apache.org/proper/commons-dbcp/configuration.html

One more reason for the same problem could be double pooling, if you have configured your connection from application server (JNDI), please ensure you are not pooling the connections both in application server and grails application. Just remove one of the pooling to solve the issue.


Monday, 15 July 2013

JDoodle.com - Online Java Compiler - Online Java Editor - Java Code Online

As a java developer I often wanted to execute a small piece of code and test it quickly without opening an IDE. Some times the same bit of code we want to execute often and use the result somewhere. May be even we want to share that bit of joy with others. One of the solutions is JDoodle.com.

JDoodle.com helps compile and execute java programs online.  It has simple interface, give a try, if found useful share with friends. Share your thoughts about JDoodle.com here in comment.

Try some default shares

Hello World
Hello World with method
Sample with import and Input Arguments

some of the features you may like is

1. auto complete import - just type the class name it will come with whole bunch of suggestions for you, just select the one you want and click Tick button.

2. Recents - you can see all the recently executed codes in the browser here. even you can ask JDoodle to remember this between same browsers different session.

3. Share - makes sharing easier. Not all your code shared by default, so you can always control what to share and what not to.