Flushing JDBC connection pools - jdbc

Does anyone know the best (or any) way to flush a JDBC connection pool? I can't find anything obvious in the documentation. It appears connection pools aren't meant to ever be deleted.
My current thought is to delete all DataSources from the hash we store them in, which will trigger our code to make new ones. However, my first attempt throws a ConcurrentModificationException.

You shouldn't be writing a connection pool. Even if you want to manage the pool yourself (as opposed to letting a container do it), you should use a library for that (such as Commons DBCP).
If you want to delete everything from a hash, you should use hash.clear().
If you want to avoid ConcurrentModificationException, you need to add synchronization.
If you delete references to Connections (are you sure you meant DataSources?), be sure to close() them first.

Why do you want to delete, rather don't create it at first place.
It should be based on your appserver, may be some JNDI programming could do the trick.

You shouldn't be writing a connection pool. That's handled by the Java EE app server.

Related

How to pool a connection in google apps script without ScriptDB?

I am looking to pool a connection in a similar way as described in this question. However I do not wish to use ScriptDB which was deprecated in May 2014.
How would this be achieved without the ScriptDB class? Not use connection pooling in Google Apps Script?
Actually, the answer in the linked question does not work as you cannot store jdbc connection objects on ScriptDb, or anywhere else for that matter. It is not possible to do a connection pool or cache in Apps Script. Even if you ´stringify´ it, when you parse back it'll not work.
One always have to create a new connection for every instance/execution of the script.

BoneCP vs WebLogic's own DB connection pool

I have a servlet which connects to Oracle DB using JDBC (ojdbc6.jar) and BoneCP. I now need to port my BoneCP-using code to something which will work in WebLogic out-of-the-box, without having BoneCP in the package.
What would be the recommended approach? What WebLogic feature I can use, specifically to get an equivalent of BoneCP's:
Performance
Ability to log failed SQL statements
Auto-resume from lost DB connection
Thanks in advance.
The best approach would be to create a standard Oracle JDBC connection pool pointing to your database. Tune it according to your necessities (number of connections, etc.). Next you would need to refactor out of your code any explicit reference to your former connection pool implementation. If you have been working with java.sql.* interfaces in your code, there should be few to no references at all.
Once all that is refactorized, you will have only a bit of code (or config file) telling your app to recover something implementing javax.sql.DataSource from a given JNDI name and getting Connections out of it. The rest should be the same - just do whatever you need and close your ResultSets, Statements and Connections as you must have been doing until now.
About your questions, you will find extensive information on how to monitor your connection pool, and its fail recovery policies, here (depending on your app server version, I paste here the one I have used):
http://docs.oracle.com/cd/E15051_01/wls/docs103/jdbc_admin/jdbc_datasources.html
About performance, I have no accurate data nor benchmarks comparing both implementations; for your tranquility, I would say you that I have never found a database performance problem in the connection pool implementation - this does not mean that it cannot exist, but it is the last place I would look for it ;)

Connection get/return listener?

Using Hibernate and Spring, my app needs to get the opportunity to manipulate the connection at the start and end of every database update. Our first guess was to override the transaction manager, but that is seeming to have some side effects from an intermittent "Pre-bound JDBC Connection found!" error to a few other harder to describe symptoms.
What is the best/easiest way to get this oppostunity. I saw someone suggest overriding the data source and then wrapping the connection, but is that really the best idea? Wrapping the connection seems dangerous, espceially since Websphere has it's own version (WSconnection).
Ideas?
Manipulation of the connection is done the easiest when a DataSource.getConnection is done, that way you know for certain that something is going to happen. So you could simply create a DataSourceProxy/-Wrapper (give it a name) which does this. There is one thing you have to take care of and that is, probably on close of the connection, that you undo the manipulation (if that is needed that is).
Instead of creating a proxy you could use some AOP to execute the code (like in this answer.

Tomcat jdbc-pool acquireRetryDelay

C3p0 cache has acquireRetryDelay parameter to set time between acquire attempts.
Has tomcat7 jdbc-pool the same functionality?
No, tomcat jdbc pool does not automatically try to connect again, when the attempt to create a connection fails. It simply will throw the exception it got as an SQLException (if it was not one in the first place).
If it's you code which needs to do retry you can do it yourself, by trying to get connection several times. Or you might be able to make a patch, it does not seem to be so hard to do.
You can try to use some different libraries, they say BoneCP is quite good (although I never tried that).

To close or not to close an Oracle Connection?

My application have performance issues, so i started to investigate this from the root: "The connection with the database".
The best practices says: "Open a connection, use it and close is as soon as possible", but i dont know the overhead that this causes, so the question is:
1 -"Open, Use, Close connections as soon as possible is the best aproach using ODP.NET?"
2 - Is there a way and how to use connection pooling with ODP.NET?
I thinking about to create a List to store some connections strings and create a logic to choose the "best" connection every time i need. Is this the best way to do it?
Here is a slide deck containing Oracle's recommended best practices:
http://www.oracle.com/technetwork/topics/dotnet/ow2011-bp-performance-deploy-dotnet-518050.pdf
You automatically get a connection pool when you create an OracleConnection. For most middle tier applications you will want to take advantage of that. You will also want to tune your pool for a realistic workload by turning on Performance Counters in the registry.
Please see the ODP.NET online help for details on connection pooling. Pool settings are added to the connection string.
Another issue people run into a lot with OracleConnections is that the garbage collector does not realize how truly resource intensive they are and does not clean them up promptly. This is compounded by the fact that ODP.NET is not fully managed and so some resources are hidden from the garbage collector. Hence the best practice is to Close() AND Dispose() all Oracle ODP.NET objects (including OracleConnection) to force them to be cleaned up.
This particular issue will be mitigated in Oracle's fully managed provider (a beta will be out shortly)
(EDIT: ODP.NET, Managed Driver is now available.)
Christian Shay
Oracle
The ODP.NET is a data provider for ADO.NET.
The best practice for ADO.Net is Open, Get Data (to memory), close, use in memory data.
For example using a OracleDataReader to load data in a DataTable in memory and close connection.
[]'s
For a single transaction this is best but for multiple transaction where you commit at the end this might not be the best solution. You need to keep the connection open until the transaction either committed or rolled back. How do you manage that and also how do you check the connection still exist in that case?(ie network failure) There is ConnectionState.Broken property which does not work at this point.

Resources