facebook

Connecting JBoss to Oracle

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #254801 Reply

    Pete CIon
    Participant

    Hello –

    I have been working with a J2EE developer for a couple of years as he’s built a relatively large financial application in JBoss (4.0.4). I have J2EE and Java experience from a few years ago, but find myself rusty at the current time. To move the project forward, I’m bringing the application into Eclipse (3.1.0). I working on MS Windows XP professional Version 2002 SP 2 and JDK version j2sdk1.4.2. My Eclipse is up to date, and I’ve gone through three or four tutorials at this point concerning J2EE database connectivity.

    I know that with the correct configuration, all should be OK, but I’m having trouble with my Oracle database connectivity. I know that I’m missing something obvious here, but have not been able to locate the problem.

    I have loaded an Oracle database 10g Express Edition onto my PC, and all is well with this. I have created a new user and a new table with data, and can connect to it both with the Web UI and with my IDE (Eclipse/myEclipse).

    I am working to connect my J2EE application to the database. To begin, I’m using a very simple JSP and class to test my connection.

    JSP:
    This is: {WORKSPACE}/Test1Web/WebRoot/jsp/TestDB.jsp

    <html>
    <head>
    <title>DB Test</title>
    </head>
    <body>

    <%
    foo.DBTest tst = new foo.DBTest();
    tst.init();
    %>

    <h2>Results</h2>
    Foo <%= tst.getFoo() %><br/>
    Bar <%= tst.getBar() %>
    Status <%= tst.getStatus() %>
    </body>
    </html>

    Java Class:

    This is: {WORKSPACE}/Test1Web/src/foo/DBTest.java

    package foo;

    import javax.naming.*;
    import javax.sql.*;
    import java.sql.*;

    public class DBTest {

    String foo = “Not Connected”;
    int bar = -1;
    String status = ” “;

    public void init() {
    try{

    Context initContext = new InitialContext();
    if(initContext == null )
    throw new Exception(“Boom – No Context”);

    status = status.concat(“\n after initContext2 \n”);

    Context envContext = (Context)initContext.lookup(
    “java:/comp/env”);

    status = status.concat(“initContext:”);
    status = status.concat(initContext.getEnvironment().toString());
    status = status.concat(“envContext:”);
    status = status.concat(envContext.getEnvironment().toString());

    DataSource ds = (DataSource)envContext.lookup
    (“jdbc/myOracle”);

    status = status.concat(“after DataSource \n”);

    if (ds != null) {
    Connection conn = ds.getConnection();

    if(conn != null) {
    foo = “Got Connection “+conn.toString();
    Statement stmt = conn.createStatement();
    ResultSet rst =
    stmt.executeQuery(
    “select id, foo, bar from testdata”);
    if(rst.next()) {
    foo=rst.getString(2);
    bar=rst.getInt(3);
    }
    conn.close();
    }
    }
    }catch(Exception e) {
    e.printStackTrace();
    }
    }

    public String getFoo() { return foo; }
    public int getBar() { return bar;}
    public String getStatus() { return status; }
    }

    Here are the changes that I’ve made:

    1. I put ojdbc14.jar file into: {JBOSS_HOME}/server/default/lib

    2. I added the following resource code into: {JBOSS_HOME}/server/default/deploy/jbossweb-tomcat55.sar/server.xml

    <Resource name=”jdbc/myOracle” auth=”Container”
    type=”javax.sql.DataSource”/>

    <ResourceParams name=”jdbc/myOracle”>
    <parameter>
    <name>factory</name>
    <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
    </parameter>
    <parameter>
    <name>driverClassName</name>
    <value>oracle.jdbc.OracleDriver</value>
    </parameter>
    <parameter>
    <name>url</name>
    <value>jdbc:oracle:thin:@127.0.0.1:1521:xe</value>
    </parameter>
    <parameter>
    <name>username</name>
    <value>pcion</value>
    </parameter>
    <parameter>
    <name>password</name>
    <value>myPassword</value>
    </parameter>
    <parameter>
    <name>maxActive</name>
    <value>20</value>
    </parameter>
    <parameter>
    <name>maxIdle</name>
    <value>10</value>
    </parameter>
    <parameter>
    <name>maxWait</name>
    <value>-1</value>
    </parameter>
    </ResourceParams>

    3. I added the following into: {WORKSPACE}/Test1Web/WebRoot/WEB-INF/web.xml

    <resource-ref>
    <description>Oracle Datasource</description>
    <res-ref-name>jdbc/myOracle</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    </resource-ref>

    Here is the problem I get when deploying:

    org.jboss.deployment.DeploymentException: Error during deploy; – nested throwable: (javax.naming.NamingException: resource-ref: jdbc/myOracle has no valid JNDI binding. Check the jboss-web/resource-ref.)

    4. I looked for jboss-web.xml, and found a number of these files within {JBOSS_HOME}/server/default/deploy. I went into one ({JBOSS_WEB}/server/default/deploy/jmx-console.war/WEB-INF/jboss-web.xml) and added the following code:
    <resource-ref>
    <description>Oracle DS example</description>
    <res-ref-name>jdbc/myOracle</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    </resource-ref>

    I continue to get this error:

    org.jboss.deployment.DeploymentException: Error during deploy; – nested throwable: (javax.naming.NamingException: resource-ref: jdbc/myOracle has no valid JNDI binding. Check the jboss-web/resource-ref.)

    Thanks so much for your help here. I like to push as far as I can, but I’ve reached my wit’s end at this point.

    Take care,

    Pete

    #254805 Reply

    Pete CIon
    Participant

    One more thing:
    With this configuration, when JBoss starts up, I do get the following confirmation:

    11:30:24,591 INFO [ConnectionFactoryBindingService] Bound ConnectionManager ‘jboss.jca:name=myOracle,service=DataSourceBinding’ to JNDI name ‘java:myOracle’

    #254808 Reply

    Pete CIon
    Participant

    And another – here is one more file that was added:
    {JBOSS_HOME}/server/default/deploy/oracle-ds.xml:

    <?xml version=”1.0″ encoding=”UTF-8″?>

    <!– ===================================================================== –>
    <!– –>
    <!– JBoss Server Configuration –>
    <!– –>
    <!– ===================================================================== –>

    <!– $Id: oracle-ds.xml,v 1.6 2004/09/15 14:37:40 loubyansky Exp $ –>
    <!– ==================================================================== –>
    <!– Datasource config for Oracle originally from Steven Coy –>
    <!– ==================================================================== –>

    <datasources>
    <local-tx-datasource>
    <jndi-name>myOracle</jndi-name>
    <connection-url>jdbc:oracle:thin:@127.0.0.1:1521:xe</connection-url>
    <!–

    Here are a couple of the possible OCI configurations.
    For more information, see http://otn.oracle.com/docs/products/oracle9i/doc_library/release2/java.920/a96654/toc.htm

    <connection-url>jdbc:oracle:oci:@youroracle-tns-name</connection-url>
    or
    <connection-url>jdbc:oracle:oci:@(description=(address=(host=youroraclehost)(protocol=tcp)(port=1521))(connect_data=(SERVICE_NAME=yourservicename)))</connection-url>

    Clearly, its better to have TNS set up properly.
    –>

    <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
    <user-name>pcion</user-name>
    <password>xmasbaby</password>

    <min-pool-size>5</min-pool-size>
    <max-pool-size>100</max-pool-size>

    <!– Uses the pingDatabase method to check a connection is still valid before handing it out from the pool –>
    <!–valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleValidConnectionChecker</valid-connection-checker-class-name–>
    <!– Checks the Oracle error codes and messages for fatal errors –>
    <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name>
    <!– sql to call when connection is created
    <new-connection-sql>some arbitrary sql</new-connection-sql>
    –>

    <!– sql to call on an existing pooled connection when it is obtained from pool – the OracleValidConnectionChecker is prefered
    <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
    –>

    <!– corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) –>
    <metadata>
    <type-mapping>Oracle9i</type-mapping>
    </metadata>
    </local-tx-datasource>

    </datasources>

    #254824 Reply

    Haris Peco
    Member

    Pete,

    SID for oracle Express is XE no xe – oracle SID is case sensitive.

    Check if your database network start correct with simple connection application like this :

    
    public class TestDriver {
    
       private static String driverName = "oracle.jdbc.driver.OracleDriver";
    
       private static String url = "jdbc:oracle:thin:@localhost:1521:XE";
    
       private static String username = "YOUR_USERNAME";
       private static String password = "YOUR_PASSWORD";
       
       public static void main(String[] args) {
          try {
             Class.forName(driverName);
             Connection connection = DriverManager.getConnection(url,username,password);
          } catch (Exception e) {
             e.printStackTrace();
             System.exit(1);
          }
         System.out.println("connection is ok");
          }
    }
    

    for driver Class name you can use oracle.jdbc.driver.OracleDriver or oracle.driver.OracleDriver (oracle.jdbc.driver.OracleDriver is newer name)

    Best regards
    Peco

Viewing 4 posts - 1 through 4 (of 4 total)
Reply To: Connecting JBoss to Oracle

You must be logged in to post in the forum log in