การ setup JNDI ใน Tomcat6 สำหรับ Progress 9.1C database

กำลังศึกษา SOA (Service Oriented Architect) เก็บเกี่ยวเทคนิคและสำรวจ application ต่างๆที่ใช้งาน และก็มีเรื่องหนึ่งคือการ integrate กับระบบ Lagacy ERP เดิมซึ่งยังจำเป็นต้องใช้งานอยู่ซึ่งมันสามารถ เชื่อมต่อด้วย Jdbc ได้ คิดว่าจะทำ Web service ให้ระบบอื่นๆสามารถเรียกใช้ข้อมูลได้

- การติดตั้ง Tomcat ไม่ยากข้ามไป

- ในส่วนของ JNDI ก็สามารถทำได้หลายวิธีก็แล้วแต่ว่าจะให้เป็น Server Pool (ใช้ร่วมกันกับทุก application) หรือ เฉพาะ application pool ซึ่งคล้ายกันต่างก้นที่ตำแหน่งการเก็บของ context.xml ซึ่งเป็น config file

-สำหรับ Server pool แก้ไขไฟล์ context.xml ที่ $TOMCAT_INSTALL_PATH/conf/

  ให้เพิ่ม element
     <Resource name="jdbc/PROGRESSDB"
            auth="Container"
            type="javax.sql.DataSource"
            username="username"
            password="password"
            driverClassName="com.progress.sql.jdbc.JdbcProgressDriver"
            url="jdbc:jdbcprogress:T:host:port:dbname"
            maxActive="20"
            maxIdle="10"
            maxWait="-1"
            logAbandoned="true"
            removeAbandonedTimeout="60"
            removeAbandoned="true"
            />

   ใน Element <Context ........>

 - ถ้าเป็น application pool ให้สร้าง folder META-INF ใน application folder อาจจะ copy หรือสร้างไฟล์ context.xml ใหม่ก็ได้ใน folder META-INF โดยเนือหาเหมือนกับทำ srever pool

 -กำหนด resource reference เพื่อให้ application สามารถเรียกใช้งานได้โดยทำการเพิ่ม

   <resource-ref>
     <description>PROGRESS JNDI Database for ERP Applications</description>
     <res-ref-name>jdbc/PROGRESSDB</res-ref-name>
     <res-ref-type>javax.sql.DataSource</res-ref-type>
     <res-auth>Container</res-auth>
   </resource-ref>
   <resource-ref>

ใน deployment descriptor ซึ่งก็คือไฟล์ web.xml นั่นเอง

- Restart Tomcat6

- การเรียกใช้

   JndiTest.java

import java.sql.Connection;
import java.sql.Statement;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.PreparedStatement ;
import java.io.* ;
import java.util.*;
import java.text.*;
//Add new  for JNDI
import javax.sql.DataSource ;
import javax.naming.Context ;
import javax.naming.InitialContext;
import javax.naming.NamingException ;

public class JndiTest {

public void main(String[] argv){

private  Connection  m_connection    = null;
try{
    String jndi_name = "jdbc/PROGRESSDB" ;    
    // Use JNDI
    Context initCtx = new InitialContext();
    Context envCtx = (Context) initCtx.lookup("java:comp/env");
    DataSource ds = (DataSource) envCtx.lookup(jndi_name);

    m_connection = ds.getConnection();

    System.out.println("JNDI WORKS!") ;
    m_connection.close() ;
    m_connection = null ;
    } catch (SQLException se) {
      System.out.println("SQL Error while connecting to the database : "+ se.toString());
    } catch (Exception ne) {
      System.out.println("Other Error while connecting to the database : "+ ne.toString());
    }

}

}