Connection Pool with Tomcat6 and Mysql

May 21, 2012 § 2 Comments

Inside $tomcat_home/conf/context.xml add this inside <Context>

<Resource name=”jdbc/datasourcename” auth=”Container” type=”javax.sql.DataSource” maxActive=”100″ maxIdle=”30″ maxWait=”1000″ username=”dbuser” password=”dbpwd” driverClassName=”com.mysql.jdbc.Driver” url=”jdbc:mysql://localhost:3306/exodus”/>

Now restart tomcat

Inside you web.xml add this before </web-app>

<resource-ref>
<description>My DB Connection</description>
<res-ref-name>jdbc/datasourcename</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

Java code to connect:

Context initCtx = new InitialContext();

DataSource ds = (DataSource) initCtx.lookup(“java:comp/env/jdbc/datasourcename”);

Connection connection = ds.getConnection();

 

About these ads

Tagged:

§ 2 Responses to Connection Pool with Tomcat6 and Mysql

  • kranthi says:

    Please write the entire java code, to connect.

  • Varun says:

    Complete code is this:

    package com.test.persistence;

    import java.sql.Connection;

    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.sql.DataSource;

    public class DBConnectionManager {

    public static final String DS_URL = “java:comp/env/jdbc/datasourcename”;

    // method to create db connection
    public static Connection getDBConnection() {
    Connection connection = null;

    try {
    Context initCtx = new InitialContext();
    DataSource ds = (DataSource) initCtx.lookup(DS_URL);
    connection = ds.getConnection();
    } catch (Exception ex) {
    ex.printStackTrace();
    throw new RuntimeException(“Unable to get connection”);
    }
    return connection;
    }

    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading Connection Pool with Tomcat6 and Mysql at Varun's Blog.

meta

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: