File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes Spring with c3p0 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring with c3p0" Watch "Spring with c3p0" New topic
Author

Spring with c3p0

ajjju kumar
Greenhorn

Joined: May 16, 2013
Posts: 9
Hello All,

I am using c3po for getting the connection pooling concept into my app .

Configured the connection pool in .xml file

and injected the ref to the bean property.


.xml
----------


Now for getting the connection i am using one class called ConnectionProvider and in that i am doing





Now if i get the connection from connectionProvider i am getting the connection thing is that in logs i am getting
As many time i get connection those many times the c3p0 pool is initilizing...





Please assist me as it may lead to leakages and perfromance decrease if i get it into production..

Thanks in advance

tummapala
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

I assume this is a JAR application? You only call ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spconfig.xml"); one time thats it. Since you are calling it multiple times you are initializing those beans multiple times. Once you have the context you typically call getBean on whatever spring managed bean kicks things off. After that calling getBean again should not be required as all your classes will be spring managed and have the dependencies they need. Also consider registering a shutdown hook so that your DS gets cleaned up when you exit the application.


[How To Ask Questions][Read before you PM me]
ajjju kumar
Greenhorn

Joined: May 16, 2013
Posts: 9
Yes you are right...So how can i get the connection object and place the connection object in context ??
should i use spring MVC dispatcher-servlet.xml..

and i should inject the object to each and every class which i need the connection object ??

Can you please help me ??
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

Now I am confused. Is this a web application (WAR to be deployed in a container like Tomcat) or a JAR application?
ajjju kumar
Greenhorn

Joined: May 16, 2013
Posts: 9
Thanks for getting me back..

its a web application and i am using jars c3po.jar for connection pool..

Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

Then you should not be creating a new application context at all. That is done by the container using the stuff you should have registered in your web.xml. Also you should not need to use the connection at all let spring manage that stuff. Use the JDBC template

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/jdbc.html

Here is a sample spring web application
https://github.com/spring-projects/greenhouse

See the web.xml
https://github.com/spring-projects/greenhouse/blob/master/src/main/webapp/WEB-INF/web.xml

You may want to get started by finding some tutorials or picking up a book like Spring in Action.
ajjju kumar
Greenhorn

Joined: May 16, 2013
Posts: 9
Thank you Bill..

i am using spring 2.5 and the sample project which you gave is quite confusing and i cannot find where the database properties are configured and how they placed in context..

Could you help in detail please ?
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

Don't use old versions of Spring. Spring 2.5 is very old. Use a current version to learn with.

I suggest you start by downloading STS.

In STS select File > New > Spring project - when you get there, you will see bunch of options under "Templates" section. One of them is Spring MVC Project. Select that and follow the directions.

You should now be able to run a very simple Spring MVC web-application. Now you just need to edit the spring context xml file to add you datasource bean. Have Spring inject the datasource into your repository bean constructor (using @Autowired) where you can use it to construct a JDBC template for accessing your database.
ajjju kumar
Greenhorn

Joined: May 16, 2013
Posts: 9
Thank you again bill you told that about STS ...

Can you please tell me this..

If suppose i use Spring MVC 2.5 i have to inject the datasource object into jdbctemplate for each and every class right ??

And if i use Spring MVC 3 and the annotation @Autowired takes care of injecting the datasource object into all the related classes right ??

Thanks in advance..
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

@Autowired exists in Spring 2.5 as well, but you should still use a new version of Spring as there are countless improvements, enhancements an bugfixes since 2.5.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spring with c3p0