7 Replies Latest reply on Apr 14, 2011 9:42 PM by lurker_huang

    How to use Jboss Datasource?

    lurker_huang Newbie

      Hi,

       

      This is the xml of my datasource:

      {code:xml}

      <datasources>

       

          <local-tx-datasource>

              <jndi-name>RaOracleDs</jndi-name>

              <use-java-context>false</use-java-context>

              <connection-url>jdbc:oracle:thin:@132.35.81.59:1521:essdemo</connection-url>

              <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>

              <user-name>top_sysmanm</user-name>

              <password>topsysmanm</password>

              <check-valid-connection-sql>SELECT 1 FROM DUAL</check-valid-connection-sql>

       

              <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name>

       

              <metadata>

                  <type-mapping>Oracle</type-mapping>

              </metadata>

       

              <min-pool-size>50</min-pool-size>

              <max-pool-size>200</max-pool-size>

              <blocking-timeout-millis>5000</blocking-timeout-millis>

              <idle-timeout-minutes>2</idle-timeout-minutes>

          </local-tx-datasource>

       

      </datasources>

      {code}

       

      when i am trying to connect database from my test class i get error

      "javax.naming.Reference cannot be cast to javax.sql.DataSource"

       

      this is my code:

       

      {code}

      package com.ailk.main;

       

      import java.sql.Connection;

      import java.sql.ResultSet;

      import java.sql.SQLException;

      import java.sql.Statement;

      import java.util.Properties;

       

      import javax.naming.InitialContext;

      import javax.naming.NamingException;

      import javax.sql.DataSource;

       

      public class DBTest {

          public static void main(String[] args) throws SQLException {

              Properties props = new Properties();

              props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");

              props.setProperty("java.naming.provider.url", "localhost:1099");

              String sql = "SELECT 1 FROM DUAL;";

              try {

                  InitialContext ctx = new InitialContext(props);

                  DataSource ds = (DataSource) ctx.lookup("RaOracleDs");

                  Connection con = ds.getConnection();

                  Statement stm = con.createStatement();

                  ResultSet rs = stm.executeQuery(sql);

                  System.out.println(rs.getString(1));

              }

              catch (NamingException e) {

                  e.printStackTrace();

              }

          }

      }

      {code}

       

      the jndi-name i found in the JBoss JMX Management Console is RaOracleDs.

       

      Please help me.