aspose file tools*
The moose likes Spring and the fly likes Spring - Connection Problem in some Unit Tests Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring - Connection Problem in some Unit Tests" Watch "Spring - Connection Problem in some Unit Tests" New topic
Author

Spring - Connection Problem in some Unit Tests

Amirr Rafique
Ranch Hand

Joined: Nov 14, 2005
Posts: 324
Hi mates

If I run all(31) the unit test together, I get following connection error in 2 - 3 unit tests, however if I run those tests indvidually, the run successfully. Can you please guide me on this strange behaviour. I am using Spring 3.0.6, Spring batch 2.1.7 and orcale XE database.

below is the error



"Know where to find the solution and how to use it - that's the secret of success."
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Are your tests closing the dataSource? calling the close() method. It looks like after running a few of the tests and those past tests shutdown Spring container but not closing the connections in the datasources, therefore the backend database has all these connections being used and is probably then reaching its max connections and can't create any new ones for the later tests.

Basically you can configure your datasource to have a <bean … destroy-method="close"/>

How are you creating your Spring ApplicationContext are you using the annotation @ContextConfiguration and @RunWith? Or are you creating it yourself in the setup method?

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Amirr Rafique
Ranch Hand

Joined: Nov 14, 2005
Posts: 324
Hi Mark

I am using @ContextConfiguration and @RunWith in the tests. I am not calling close() method of DataSource. My understanding was that all the connection handling is taken care by Spring itself. I have tested it by adding the close() call but of no gain. Below is my datasource config
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

How many connections does your database allow. Can you post some of your test code? The cause network connection could not be made would be either because the network went down, but I doubt that, or that you are trying to create too many connections than the server/database can start up. So it isn't exactly a Spring or Unit Test problem as a configuration for your database.

What database are you using?

Mark
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

In addition to what Mark said, consider providing the following information

1. Does the exception happen on the same test when you run all 31 test cases?
2. Does it happen each and every time you run the 31 test cases?
3. Do all the 31 cases try to connect to the database?

31 test cases (unit test) connecting to the database? How many steps does your batch job have? Do you really need so many to connect to the Database?


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Amirr Rafique
Ranch Hand

Joined: Nov 14, 2005
Posts: 324
Mark Spritzler wrote:How many connections does your database allow. Can you post some of your test code? The cause network connection could not be made would be either because the network went down, but I doubt that, or that you are trying to create too many connections than the server/database can start up. So it isn't exactly a Spring or Unit Test problem as a configuration for your database.

What database are you using?

Mark


I am using Orcale XE database installed on my local machine(No network db call). I am not sure how I should check the connection allowed by the db. Please guide me on this. It is the case of connection exhausting but I am not sure where is the problem. I my unit test I am get simpleJdbcTemplate injected. My unit test are calling Spring Batch Job. The job reads table containting 100 reacords, call some transformation logic and write them back in new table.
Amirr Rafique
Ranch Hand

Joined: Nov 14, 2005
Posts: 324
If I run unit test from maven, they work fine. The problems only seems with tests run from eclipse.

Any idea ?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Amirr Rafique wrote:If I run unit test from maven, they work fine. The problems only seems with tests run from eclipse.

Any idea ?


Yeah, Eclipse. ;) You have managed to reduce the possibilities to one place. Eclipse.

Also, the latest you post you wrote sounds like a different question. I am going to split that into its own thread here.


Mark
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Amirr Rafique,
Your post was moved to a new topic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spring - Connection Problem in some Unit Tests