[Logo] JavaRanch » Big Moose Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » JDBC
 
RSS feed
 
New topic
Author

Synchronize DataSource Instance?

kavin clain
Ranch Hand

Joined: Jan 19, 2010
Messages: 42

After configuring web.xml & context.xml I am using below code to get Connection. "DBBoard.getConnection()"

Do I need to synchronize on DataSource instance... ?

This message was edited 1 time. Last update was at by kavin clain

Peter Johnson
author
Bartender

Joined: May 14, 2008
Messages: 2384

Is there a reason why you wrote this instead of making use of the database connection pool? Using the pool is the best thing to do within a Java EE app.

JBoss In Action
Deepak Bala
Bartender

Joined: Feb 24, 2006
Messages: 4876

Most containers provide their own connection pool. Tomcat for example can use commons DBCP to manage connections. Which container do you use and why dont you try to use its connection pooling mechanism ?

SCJP 6 articles - SCJP 5/6 mock exams - SCJP Mocks - SCJP 5 Mock exam (Word document ) - SCJP 5 Mock exam in Java.Inquisition format
kavin clain
Ranch Hand

Joined: Jan 19, 2010
Messages: 42

I am using Tomcat 5.5. How to use the connection pool & get connection & after using it how to release it?


In my project folder I created META-INF folder then I wrote context.xml inside the folder.


I was not sure but I wrote the above class(in previous message) to provide access to the datasource.

Then I did one entry in web.xml:


This message was edited 1 time. Last update was at by kavin clain

Deepak Bala
Bartender

Joined: Feb 24, 2006
Messages: 4876

Simply close the connection after you are done. The Connection pool is responsible for reclaiming it.

SCJP 6 articles - SCJP 5/6 mock exams - SCJP Mocks - SCJP 5 Mock exam (Word document ) - SCJP 5 Mock exam in Java.Inquisition format
kavin clain
Ranch Hand

Joined: Jan 19, 2010
Messages: 42

Thank you Deepak.

I state three things here: one the DBBoard class providing DataSource access to get Connection. and other is configuration of DataSource.

Could you please tell me is it not the correct way to use Tomcat 5.5 connection pool? If not then what is the correct way?
As in previous message by you and Peter suggesting to use Connection Pool provided by container, I think I made some mistake here the way accessing the connection pool.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Messages: 4876

Your first post did not mention a DBCP framework working to back the data source. Which is why we raised the point.

SCJP 6 articles - SCJP 5/6 mock exams - SCJP Mocks - SCJP 5 Mock exam (Word document ) - SCJP 5 Mock exam in Java.Inquisition format
kavin clain
Ranch Hand

Joined: Jan 19, 2010
Messages: 42

Thanks again. It means the configuration part is correct.
And what about the class to getEnvironmentContext, getDataSource & getConnection. Is this implementation correct? please suggest if any changes need to incorporate!

Do I synchronize the getConnection method.

public static syncronized Connection getConnection(){...}

This message was edited 1 time. Last update was at by kavin clain

 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » JDBC
 
RSS feed
 
New topic
MyEclipse Enterprise Workbench

.