• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Spring with c3p0

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
ajjju kumar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for getting me back..

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

 
Bill Gorder
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic