Providing connection time out in Derby client driver - java

I have a requirement that retrieving a connection times out within specified time from the connection pool if there is any problem in the network. This should be handled through a java application.
Basically, want to give timeout to datasource.getConnection() method.
So after some research, I found setLoginTimeout(int seconds) method of DataSource has this functionality in order to notify if a database connection can not be made within the specified seconds.
I applied this on OracleConnectionPoolDataSource which is working fine, but for the Derby client driver it's giving exception as,
org.apache.derby.jdbc.ClientDriver does not have any such attribute loginTimeout.
Looking for your kind responses, please do let me know if there any other way to achieve this as per my requirement or anything I need to add/missing or any information related to this.
Server - Resin 3.0

finally able to achieve this by using c3p0 connection pooling instead of using datasource for derby.C3P0 has checkoutTimeout property which exactly works as per this requirement.

Related

ORA-02020 – Too many database links in use

i have a web based application which uses weblogic connection pooling while accessing oracle db. there is a procedure in which i must use db link to fetch and update remote table. but after a few call my service which triggers procedure call i got ORA-02020 – Too many database links in use error.
I think it causes from weblogic connection pool, it does not close session so db link is not closed and it reached max dblink count after a few try.
i've found http://dbtricks.com/?p=198 work around solution but it have not worked for me. i must use db link so what should i do? is there any solution for my case?
thanks.

Some queries causes BBDD Closed connection

I have a web app deployed on a Jboss EAP 6 that uses Hibernate to access a SQL Server database.
Every time I look for a specific register I get the error:
com.microsoft.sqlserver.jdbc.SQLServerException: The connection is closed
Search for any other register works fine. Restarting JBoss seems to solve the problem. Now I can get the register data without problem.
What could be causing the Exception for that register? Should I focus on the code? On the network? On the datasources definition? On the DB configuration?
There are many reasons, but very offten:
Look at your code, you maybe call close() method
Check out your connection string (NTLMv2?)
Check out MS SQL Server and their setting, DB servers looking for not active sessions and closing them.
Below reasons can be possible
Long query which is hanging up Sql Server.
Using Too much joins in query.
Tables contain more data and you have make the query in which join is not proper.

How to Create A Connection Pool/Data Source rather than connecting to A SQL DB

I have code that connects to a SQL DB and queries it based on user input. I would like to connect using a Pool instead to speed up the query times. I attempted to write a Pool and Manager class but I am getting an unreported NamingException when I try to get a connection from the pool. I have also already caught NamingExceptions in my getConnection() function.
Does anyone know why I am getting this error?
Or could point me in the right direction to create a valid ConnectionPool?
You should use the latest JDBC driver, which supports connection pooling internal, so you don't have to write your own.
You have 3 options:
If your code runs in an application server, configure a connection pool in the server.
Some JDBC drivers provide a connection pool implementation (but many only provide a poolable data source that is meant for integration with an app server).
Use commons-dbcp or some equivalent library to create a connection pool.

Can I change the 'appName' on an already open JTDS connection?

I'm looking for a way to pass web-application transaction information on to the underlying database process. In my Java code I might have a transactional method ReservationService#search(), which runs one or several SQLs. On the DBMS I just see a SPID along with some locks. I'm looking for a way to add a tag "ReservationService#search" to the database process.
jTDS / Sybase ASE have an appName which can be passed in as a connection property. As we're using a connection pool, existing connections are re-used, but to my knowledge the appName is only read on establishing a new connection.
How can I re-set the appName on an already existing connection (without closing/opening)? Or, if that simply is impossible, are there any other ideas to get transactional context information from Java to the DBMS?
Tomcat Webapplication (Java 6)
C3P0 Connection Pool (only supports JDBC 3)
jTDS connecting to Sybase ASE 15
Thanks
Simon
Unfortunately not, it seems that you can only specify that in the URL parameters when you open up the connection but can't be altered afterwords.
You can aways pass in a SessionID of some kind from your Java/Tomecat to all your Sybase queries. For me, this was easy as I use stored procedures for all communications between my Java application and the SQL server. I based my SessionID in Java on the J2EE session.

Connection with MySql is being aborted automatically. How to configure Connector/J properly?

I read this advice from error message:
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.
I'm using Spring and JPA. Where should I configure Connector/J? (in persistence.xml, or in entityManagerFactory spring configuration, or in dateSource spring configuration, or somewhere else?)
The text describes three solutions to prevent connection aborts:
Configure the connection string with autoReconnect=true. This is a property of the URL connection string, which works at the driver level. You need to change the connection string in the data source configuration.
url="jdbc:mysql://localhost:3306/confluence?autoReconnect=true"
Increase the timeout. This is normally a property of the database. You can increase this value to see if you get less connection abort.
Configure the connection pool to test the connection validatiy. This is done at the pool, not a the driver level. This will depend on the data source implementation that you use. But it should be configurable in the property of the data source, if you use a pooled one, e.g. c3p0.
Connection pooling options with JDBC: DBCP vs C3P0
http://snipplr.com/view/14725/c3p0-datasource-config-connectionpool/
http://www.mchange.com/projects/c3p0/index.html#configuring_connection_testing
Additionnal comments:
The datasource/pool can also have a timeout, which corresponds to the time an idle connection remains in the pool. To not confused with the db timeout.
There are several way to test the validity of a connection. One common way is to have dummy test table. The pool will issue a select on the dummy test table to see if the connection is still OK.
AutoReconnect is not recommended. From MySQL here
Should the driver try to re-establish stale and/or dead connections?
If enabled the driver will throw an exception for a queries issued on
a stale or dead connection, which belong to the current transaction,
but will attempt reconnect before the next query issued on the
connection in a new transaction. The use of this feature is not
recommended, because it has side effects related to session state and
data consistency when applications don't handle SQLExceptions
properly, and is only designed to be used when you are unable to
configure your application to handle SQLExceptions resulting from dead
and stale connections properly. Alternatively, as a last option,
investigate setting the MySQL server variable "wait_timeout" to a high
value, rather than the default of 8 hours.
I was go through many solutions and my problem was solved but after some time the connection is timeout or disconnected.After 2 3 days I got a solution that solve my problem.
many solution suggest to use autoReconnect=true but when I was go through the docs. I saw the following text in the source describing the autoReconnect parameter:
The use of this feature is not recommended, because it has side effects related to session state and data consistency
When I looked in the Hibernate code. The basic connection mechanism of Hibernate doesn’t support reconnecting, one has to use H3C0 connection pool (which itself didn't always support reconnecting).
But once one’s used H3C0, the default behavior seems to be that to process a request, if the connection is dead then the user sees and error - but at least it reconnects for the next request. I suppose one error is better than infinite errors, but still not as good as zero errors. It turns out one needs the optiontestConnectionOnCheckout- which the documentation doesn’t recommend because testing the connection before a request might lead to lower performance. Surely the software firstly has to work, only secondly does it have to work fast.
So, to summarize, to get a connection to “work” (which I define as including handling dropped connections by reconnecting without error): In “hibernate.cfg.xml”:
<!-- hibernate.cfg.xml -->
<property name="c3p0.min_size">5</property>
<property name="c3p0.max_size">20</property>
<property name="c3p0.timeout">1800</property>
<property name="c3p0.max_statements">50</property>
<!-- no "connection.pool_size" entry! -->
Then create a file “c3p0.properties” which must be in the root of the classpath (i.e. no way to override it for particular parts of the application):
c3p0.properties
c3p0.testConnectionOnCheckout=true
If this solution don't work than there are more possible solutions:-
1. Add
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
Also dont forget to place the c3p0-0.9.1.jar in the classpath.
2. Instead of using that c3p0.properties file, couldn't you just use this property in your hibernate.cfg.xml:
<property name="hibernate.c3p0.validate">true</property>
Also checkout the last post on this page:
https://forum.hibernate.org/viewtopic.php?p=2399313
If all these not work than go [more][1] and read in detail
[1]: http://hibernatedb.blogspot.in/2009/05/automatic-reconnect-from-hibernate-to.html
This is for people like me who find this old posting through the search engines.
The other answers are better long term solutions. But if you just need the mysql connection running again right away, you can shutdown then restart tomcat and everything will work fine for a while. This enables you to avoid system downtime while you figure out a longer term solution.
Navigate to $CATALINA_HOME in the terminal, then type shutdown.sh, then type startup.sh. Wait a few moments for the startup sequence to complete, then your apps will work again for a while.

Categories