Showing posts with label Datasource Lookup. Show all posts
Showing posts with label Datasource Lookup. Show all posts

Thursday, 27 August 2015

Configure Oracle DataSource in Tomcat

Here is the procedure, how to configure Oracle datasource in Tomcat and lookup the same.

     1. Get Oracle driver

Download Oracle driver from here and copy it to $TOMCAT\lib folder

     2. Create META-INF/context.xml and add following entry.

 <?xml version="1.0" encoding="windows-1252" ?>
<Context override="true">

    <Resource name="jdbc/hr" auth="Container"
        type="javax.sql.DataSource" maxActive="100" maxIdle="30"
        maxWait="10000" username="hr" password="welcome1"
        driverClassName="oracle.jdbc.driver.OracleDriver"
        url="jdbc:oracle:thin:@localhost:1521:orcl" />   
    </Context>



    3. web.xml configuration

<?xml version = '1.0' encoding = 'windows-1252'?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5" xmlns="http://java.sun.com/xml/ns/javaee">
   
  <resource-ref>
    <description>Oracle Datasource</description>
    <res-ref-name>jdbc/hr</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>
</web-app>



  4. Lookup datasource via context lookup service

Below class lookup datasource and established connection with Oracle DB

import javax.naming.Context;
import javax.naming.InitialContext;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class GetConnection {

    private static DataSource ds;

    public static DataSource getDS() {
        try {
            Context ctx = new InitialContext();
            ds = (DataSource)ctx.lookup("java:comp/env/jdbc/hr");
        } catch (NamingException e) {
            e.printStackTrace();
        }
        return ds;
    }

    public static void main(String[] args) {
        try {

            DataSource ds = GetConnection.getDS();

            Connection connection = ds.getConnection();
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        }
    }

}




Wednesday, 26 August 2015

Java Jndi datasource lookup

JNDI style is typical when using an application server(Weblogic, JBOSS) or a web container(Apache Tomcat).

Here's a example of the lookup Datasource created in Server.

import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DatabaseConnection {
    public DatabaseConnection() {
        super();
    }
    // JNDI name created in Server

    final static String DATA_SOURCE="jdbc/PortalDS";
    public static void main(String[] args) {

        Connection connection = DatabaseConnection.init(DATA_SOURCE);
       
        System.out.println("Connection "+connection);

    }
   
    private static Connection init(String dsName) {
              Connection con = null;
              DataSource datasource = null;
      
              Context initialContext = null;
              try{
                initialContext = new InitialContext();
                if (initialContext == null) {
                }
                datasource = (DataSource)initialContext.lookup(dsName);
                if (datasource != null) {
                     con = datasource.getConnection();
                } else {
                    System.out.println("Failed to lookup datasource.");

                }
              }catch (SQLException sqle) {
                     System.out.println("SQL Exception occurred while getting DataSource : " +
                               sqle);
                    
                 } catch (NamingException ne) {
                     System.out.println("Naming Exception occurred while getting DataSource : " +
                               ne);
               

                 }
            
              return con;
          }
}