3
DriverManager.getConnection("jdbc:mysql:localhost:3306/testdb","root","root");
    (or)
DataSource ds = new {some class which implements DataSource interface};
ds.getConnection("root","root");

Using DataSource is preferred over DriverManager.getConnection();

Every tutorial suggest to use DataSource since it is having some advantages over DriverManager. As far as i know, (correct me if i am wrong) connection pooling is the main benefit we will get in DataSource(may be other benefits also).

In that case, if my requirement is i need connection pooling in my Desktop java application then how can i implement that?. Please dont confuse me with JNDI blah blah....

is it something like which database i'm using (mysql) that vendor should provide a class that implements DataSource interface. eg: MysqlDataSource.

if so... will the below code is correct...? and does it provide me the connection pool benefit? how can i ensure that?

MysqlDataSource ds = new MysqlDataSource();
ds.setUrl("jdbc:mysql://localhost:3306/testdb");
ds.setUser("root");
ds.setPassword("root");
Connection connection = ds.getConnection();
ᗩИᎠЯƎᗩ
  • 2,122
  • 5
  • 29
  • 41
Kumar
  • 71
  • 1
  • 3
  • possible duplicate of [java - DataSource for standalone application - no application server](http://stackoverflow.com/questions/15588449/java-datasource-for-standalone-application-no-application-server) – KhAn SaAb Sep 18 '13 at 07:40
  • Apologies if I'm mistaking, but my firs impression is that you should understand what a pool is http://en.wikipedia.org/wiki/Object_pool_pattern. After this you can decide if and what implementation you'd like to use, eg apache commons, c3p0, etc, or create your own – Morfic Sep 18 '13 at 07:42
  • The code you've posted won't create a connection pool. You need to create the `MysqlDataSource` like that, but then wrap it in a pooling data source of some sort. The question @nAvEeD links to has answers with links to pooling data sources you could use. The documentation for those pooling data sources will tell you everything you need to know. – Tom Anderson Sep 18 '13 at 08:14
  • Ok. So wat u r saying is just create a data source and then link it with a pooling source. Cooooool. Thanks. Can u have a look at my below question. – Kumar Sep 18 '13 at 08:55
  • A `DataSource` implementation doesn't necessarily provide connection pooling. – Mark Rotteveel Sep 18 '13 at 09:08

2 Answers2

1

I used Tomcat Pool Datasource. If you need database connection in a standalone application (outside of an application or web server), you can try this one.

<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-jdbc</artifactId>
    <version>7.0.35</version>
</dependency>


DataSource ds = new DataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3307/pcmDB");
ds.setUsername("user");
ds.setPassword("password");
ds.setInitialSize(5);
ds.setMaxActive(10);
ds.setMaxIdle(5);
ds.setMinIdle(2);

Some references

http://www.tomcatexpert.com/blog/2010/04/01/configuring-jdbc-pool-high-concurrency

http://www.codingpedia.org/ama/tomcat-jdbc-connection-pool-configuration-for-production-and-development/

Yan Khonski
  • 12,225
  • 15
  • 76
  • 114
-2

Try 3rd party lib.

You can look at http://jolbox.com/ it's easy to learn and used.

Peerapat A
  • 420
  • 4
  • 13