File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes static connections and connection pooling - what happens when physical connection is lost? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "static connections and connection pooling - what happens when physical connection is lost?" Watch "static connections and connection pooling - what happens when physical connection is lost?" New topic
Author

static connections and connection pooling - what happens when physical connection is lost?

Johach Zinia
Greenhorn

Joined: Nov 03, 2009
Posts: 2

My EJB has a method which will be running on a transaction started by a previous EJB (which is calling this one, having transaction attribute set to Required).

This method has multiple calls to a makeValid method that needs to commit separately from the main transaction:




I will have multiple threads acceding myMethod and I need them to see each other changes to the database occurring in the makeValid method, so I have declared makeValid method as static synchronized.
Also I want to avoid creating a new connection each time makeValid is called, so I have created a static reference for that 2nd DB connection.


My EJB uses the getConnection method from a helper class that goes like:



I am not sure about the connection management that the application server will be doing, so I would appreciate comments on the following items:

1- If I use a static variable to hold reference to this connection, the connection reference will be unique; after I invoke "getConnection", this unique reference will be tied to a physical connection that the application server will get from the connection pool, so that the physical connection used in the application will also be unique

2- the first time I run the application, as conn in the ConnectionHelper class is null, the getConnection method will return a new connection; but the next time I execute the application, as the static staticConnection still "lives", conn in the ConnectionHelper class will no longer be null, so that invoking getConnection will always be returning the same physical connection

3- If the physical connection is lost, I will have a valid reference tied to an invalid connection

4- As the connection is static, only when the application server is restarted I will be able to get a new valid connection

5- How could I guarantee that a valid physical connection is used each time the EJB is invoked?

6- When I create a 2nd connection object like this:
Connection specialPurposeConnection = (new ConnectionHelper()).getConnection();
the actual physical connection being used (got from the connection pool) may be the same that was returned for mainConnection, so when I will be invoking
specialPurposeConnection.commit();
I may unwantedly commit everything from both mainConnection and specialPurposeConnection.

I suppose some of these are wrong...
Thank you in advance for help on this topic.



Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29233
    
135

Johach,
Welcome to JavaRanch!

The short answer is you are asking by trouble with this. A static connection will be useless shortly as it will eventually get closed or time out. An instance variable connection could have the same problem. This is why people use connection pools.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Johach Zinia
Greenhorn

Joined: Nov 03, 2009
Posts: 2
So you mean my sentences 1 to 5 are true?

Regarding sentence 6, as the first connection still hasn't been committed or rolled back, it hasn't been returned to the connection pool, so if I invoke getConnection to get a 2nd connection this will actually be a different physical connection and it will commit separately from the other one (so sentence 6 is wrong). Is this correct?


Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2476
    
    7

as the first connection still hasn't been committed or rolled back, it hasn't been returned to the connection pool
Slight nuance: Committing or Rollback does not return the connection to the pool. Closing the connection returns it.
But your findings are correct. Your second connection runs in isolation from the one that is still in use, and will not commit or roll back changes made in the first one.

Regards, Jan


OCUP UML fundamental and ITIL foundation
youtube channel
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static connections and connection pooling - what happens when physical connection is lost?
 
Similar Threads
JDBC Question
JDBC-connection
DBUtility for closing connections
Single tone Vs Static
ClassNotFoundException while trying to connect to Oracle