I am assuming you are talking about your development environment.
The connection pool has timed out, therefore you are getting an error on the first invocation and a success on the second invocation. Once you will move to a production environment I assume you will not have hours of inactivity, and if you will you can configure the connection policy.
For example for WebSphere Application Server Liberty Profile configuration Edit the server.xml
file and for every <dataSource>
element add a <connectionManager>
element with the agedTimeout property: <connectionManager agedTimeout="timeout"/>
For example:
<dataSource jndiName="jdbc/AppCenterDS" transactional="false">
<connectionManager agedTimeout="7h30m"/>
<jdbcDriver libraryRef="MySQLLib"/>
...
</dataSource>
See more information at: https://pic.dhe.ibm.com/infocenter/wrklight/v6r1m0/topic/com.ibm.worklight.installconfig.doc/install_config/c_handling_stale_mysql_connections.html