How to reduce hang time of failed DB connection - java

I have a java web application that connects to a MSSQL 2005 database using Hibernate.
As the connnection to the DB from my application is not critical I wanted to test how the application behaved when the database was unavailable.
When I stop the SQL services and run through a part of the web application that runs queries on the database, my application hangs for about 30 seconds that then continues to function as normal.
As expected the error in the logs is:
2012-08-13 16:33:04,974 WARN [JDBCExceptionReporter] SQL Error: 0, SQLState: 08S01
2012-08-13 16:33:04,974 ERROR [JDBCExceptionReporter] The TCP/IP connection to the host bonnie.uk, port 1433 has failed. Error: "Connection refused. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".
Does anyone know how I can reduce the hangtime of the application?
Thanks
Shaw

As you mentioned that it's a web application, I suppose you are using a DataSource provided by your container (Tomcat, JBoss AS, ...), right? Right?? :-)
If that's the case, then your container certainly has a way to configure this. For JBoss AS, this would be "blocking-timeout-millis".

Related

IO Exception: Connection Reset Error from jvm to db

I have two application servers distinctly located in two different data centers, running the application in active-active mode. The application db is also hosted between the same two data center in active-passive mode. I am receiving connection reset errors from my application server which is on the other datacenter, when connecting to the DB. These connection reset errors are intermittent and no ORA/Java exception codes are provided with it. ht e datacenter diagram is provided here
enter image description here
java.sql.BatchUpdateException: Io exception: Connection reset
A network appliance somewhere between your app server and the Database may kill the socket based on inactivity. This happens with large connection pools where all connections aren't used frequently. It could be resolved by turning keep_alive on all the JDBC connections. To do so set the JDBC property "oracle.net.keepAlive" to "true".

cannot connect to sql server 2012 with java jdbc

I am learning SQL/SQL Server 2012. I am also learning how to make forms with java using NetBeans. I am trying to link java to my SQL Server 2012 Express so I can make a form that uses/updates data in a SQL database but have not been successful.
I have done lots of research trying to find a solution and made changes that I found I needed to make. I have set up SQL Server to use TCP port 1433 on my computer. I have also made an exception in my firewall to allow that incoming connection. I have created a user "admin" and given that user sysadmin role. I have also set up SQL Server to allow remote connections with a time out of 60 seconds.
When I try to connect to the SQL Server from MS SQL Server Management Studio and put my computers static IP and port 1433, I get the attached error:
When I try to connect to the SQL Server database from NetBeans I get this:
Cannot establish a connection to
jdbc:sqlserver://DEVINSPC\SQLEXPRESS;databaseName=Test using
com.microsoft.sqlserver.jdbc.SQLServerDriver (The TCP/IP connection to
the host DEVINSPC, port 1433 has failed. Error: "connect timed out.
Verify the connection properties. Make sure that an instance of SQL
Server is running on the host and accepting TCP/IP connections at the
port. Make sure that TCP connections to the port are not blocked by a
firewall.".)
I am using jdbc4.2.jar file. I have set SQL Server to use both SQL Server and Windows Authentication. Hopefully this all makes sense.
If you need anymore info let me know. Any help is appreciated!

Hibernate app can't connect to MySQL after MySQL reboot

I have a webapp that is running on amazon ec2 on tomcat with hibernate and rest, my mySQL is standalone instance through amazon rds.
Once i start my webapp - everything is working fine, but recently i configured daily backups on my database and then started seeing problems with my webapp connecting to mySQL.
Basically the problem is only happens if my webapp was started before i mysql instance was restarted(backed up). Then after mySQL restart for some reason any connections to it from my webapp are failing.
It all resolves once i restart my ec2 vm (It might resolve if i restart tomcat as well, but i haven't tried that)
How can i make sure my webapp gets connected back to the mysql after mysql restart?
This is what im getting written to my log:
21-May-2015 11:42:27.857 WARN [http-nio-8080-exec-2] org.hibernate.engine.jdbc.spi.SqlExceptionHelper.logExceptions SQL Error: 0, SQLState: 08S01
21-May-2015 11:42:27.857 ERROR [http-nio-8080-exec-2] org.hibernate.engine.jdbc.spi.SqlExceptionHelper.logExceptions Communications link failure
The last packet successfully received from the server was 200,187 milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago.
Any suggestions on what to dig into?
You should use a connection pool. For Hibernate, you can use c3p0.
In your hibernate properties set the following
hibernate.connection.provider_class = org.hibernate.connection.C3P0ConnectionProvider
Then, in a c3p0.properties file, put these properties to retry to reconnect indefinitely every 3 seconds when database is down:
c3p0.acquireRetryAttempts = 0
c3p0.acquireRetryDelay = 3000
c3p0.breakAfterAcquireFailure = false
See this section for more details on how to recover from a database outage.

java.sql.SQLException: Io exception: Broken pipe how to recover without restart?

In my application I use connection to Oracle, when connection lost and I try to re-connect I receive exception:
java.sql.SQLException: Io exception: Broken pipe
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:124)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:161)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:273)
at oracle.jdbc.driver.T4CStatement.fetch(T4CStatement.java:540)
at oracle.jdbc.driver.OracleResultSetImpl.close_or_fetch_from_next(OracleResultSetImpl.java:264)
at oracle.jdbc.driver.OracleResultSetImpl.next(OracleResultSetImpl.java:196)
For recover I need to restart application, does it possible recover without restart?
Thanks.
Followings could be the possibilities which could be causing the exception:
Network problem: That is the network between the database and application server causing the physical connection to be dropped after a period of time. It's probably due to a firewall running behind the network which is configured to kill db connections after a specified period of time. You may consider a workaround to maintain the connection alive all the time simply by re-configuring your application server. For Tomcat, you may try adding validationQuery="select 'validationQuery' from dual in the Tomcat datasource conf file (context.xml)
The connections to the database server are being reset and the client is not notified by the database driver. The problem in this case is that the Oracle driver is discovering that it's socket to the DBMS somehow (firewall again, maybe?) has been closed by the other end. You may consider setting your connection timeout (in the pool) shorter than the network/DB server timeout as a solution.

Proxy error while accessing the application

I have an apache proxy between browser and my custom web server. So after starting the webserver, I can access my web application for like 4-5 hours. After that I get this error
Proxy Error The proxy server received an invalid response from an
upstream server. The proxy server could not handle the request GET /.
Reason: Error reading from remote server
To access the web application again, I have to restart the web server. I tried to check the log I got from my web server and only error in that log , I am finding is this
com.mysql.jdbc.CommunicationsException: Communications link failure
due to underlying exception:
** BEGIN NESTED EXCEPTION **
java.net.SocketException MESSAGE: Connection timed out
STACKTRACE:
java.net.SocketException: Connection timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:146)
at com.mysql.jdbc.util.ReadAheadInputStream.fill(ReadAheadInputStream.java:113)
at com.mysql.jdbc.util.ReadAheadInputStream.readFromUnderlyingStreamIfNecessary(ReadAheadInputStream.java:160)
at com.mysql.jdbc.util.ReadAheadInputStream.read(ReadAheadInputStream.java:188)
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1910)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2304)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2803)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3170)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3099)
at com.mysql.jdbc.Statement.execute(Statement.java:695)
at hra.database.Pool.getConnection(Pool.java:62)
** END NESTED EXCEPTION **
Last packet sent to the server was 925624 ms ago.
And I tried to fix this database error, but I still get the above mentioned proxy error, so definitely the log information is not useful.
This is what I am getting from Apache Proxy log
(70007)The timeout specified has expired: proxy: error reading status
line from remote server
Does anyone know what can be the issue or any helpful pointers to fix this issue?
apache proxy fails because customwebserver does not answer. your
customwebserver logs a crash waiting for mysql to answer. rebooting
customwebserver fixes it, so mysql is not the problem. it looks like the problem the way your customwebserver handles its connections to mysql.
can we see your jdbc config? do you have autoReconnect=true ?
What is likely happening is that your up-stream server is shutting itself down because of inactivity. Find the setting on the server that controls that and turn it up.
I can see that you see two connection timeout errors
Apache Proxy Web server timing out while connecting to custom web server
Custom web server timing out while connecting to database
If the timeout set on proxy server is less than timeout set for database connection, before the database connection times out, proxy server would time out as well.
As a first thing resolve all DB connectivity issues and test your application by directly connecting to custom web server (not via proxy).
Observe and identify normal response time and max response time.
Set proxy time out ProxyTimeout to be a value that is more than (max response time) + (network delay)
If possible you may share proxy server configurations to help you further.
I think like stated by the previous answer that you encounter a timeout raised by one of the members of your infrastructure:
server
network device (routers are clever beasts but firewalls can be clever too)
Did you setup a real connection pool ? Using heart beat mechanism to keep alive connections ?
This induces some network traffic but avoids such problems ?
Try to use a network capture tool to have many logs to analyze
HTH
jerome

Categories