Forum Stats

  • 3,814,604 Users
  • 2,258,890 Discussions
  • 7,892,780 Comments

Discussions

Programmatically accessing DBCS using java- javax.naming.CommunicationException

3032683
3032683 Member Posts: 25
edited Jun 27, 2017 2:58AM in Java Cloud Service

I have a java program which will be deployed to JCS. We have created one table which will be used by our java program.

To interact with DBCS, i have created data source in my jcs weblogic console and trying to access as shown below

OracleConnection conn=null;

    javax.sql.DataSource ds=null;

    Hashtable env = new Hashtable();

    env.put( Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory" );

    env.put(Context.PROVIDER_URL, "https://<host>:7002");

    try{

      Context context=new InitialContext( env );

      //you will need to have create a Data Source with JNDI name testDS

      ds=(javax.sql.DataSource) context.lookup ("jdbc/testDS");

      conn=(OracleConnection) ds.getConnection();

      java.util.Properties prop = new java.util.Properties();

      System.out.println("Connection object details : "+conn);

      conn.close();

    }catch(Exception ex){

      //handle the exception

        System.out.println("Exception "+ ex);

    }

. But I am getting below mentioned error

Exception javax.naming.CommunicationException: https://<host>:7002: [RJVM:000575]Destination <host>, 7002 unreachable.; nested exception is:

  javax.net.ssl.SSLHandshakeException: General SSLEngine problem; [RJVM:000576]No available router to destination.; nested exception is:

  java.rmi.ConnectException: [RJVM:000576]No available router to destination. [Root exception is java.net.ConnectException: https://<host>:7002: [RJVM:000575]Destination <host>, 7002 unreachable.; nested exception is:

  javax.net.ssl.SSLHandshakeException: General SSLEngine problem; [RJVM:000576]No available router to destination.; nested exception is:

  java.rmi.ConnectException: [RJVM:000576]No available router to destination.]

Could any one please suggest me the reason for above issue?

Thanks in advance.

Kotresh

Best Answer

  • SanjeevChauhan
    SanjeevChauhan Member Posts: 1,554 Gold Trophy
    edited Jun 27, 2017 2:18AM Answer ✓

    How are you testing your code?

    My assumption is

    1. you have jndi connection created on weblogic.

    2. You are deploying your code on same weblogic and then you are testing it.

    I suspect you are just trying to run your java file. In that case code is not deployed on weblogic and it runs as standalone java program. It may not be able to use jndi created in weblogic server.

    You may want to use hard coded details in that case.

        public Connection getConnection(){

            try {           

                DriverManager.registerDriver (new oracle.jdbc.OracleDriver());

                String connectionUrl = "jdbc:oracle:thin:<user>/<password>@//&lt;host>:<port>/<sid-or-service>";

                OracleConnection conn = (OracleConnection)DriverManager.getConnection(connectionUrl);

                conn.setAutoCommit(false);

              return conn;

            } catch (Exception e) {

               e.printStackTrace();

               DiagnosticUtil.printStackTrace(e);

               throw new RuntimeException(e.getMessage());

            }

        }

    My suggestion would be to test standalone java you can use above method. The moment you decide to upload war/ear which needs connection inside use my previous post method.

    Thanks

    Sanjeev

Answers

  • 3032683
    3032683 Member Posts: 25
    edited Jun 23, 2017 3:27AM

    Hi All,

    I have tried with t3://<host>:7002  instead of  https://<host>:7002 but now I am getting, Exception e javax.naming.CommunicationException: [Login failed for an unknown reason.

    I have added below properties and trying

    env.put(Context.SECURITY_PRINCIPAL, "weblogic");

        env.put(Context.SECURITY_CREDENTIALS,"welcome1"); where weblogic is my username and welcome1 is my password. 

    is there anything additional details i have to put. please suggest.

    Thanks

  • SanjeevChauhan
    SanjeevChauhan Member Posts: 1,554 Gold Trophy
    edited Jun 25, 2017 9:12PM

    You can follow these steps

    1. Create a JNDI connection in your weblogic and make sure you are able to successfully test connection. Let say jndi is jdbc/mydb

    2. In your java code (which is getting deployed on jcs), you can get connection simply using below lines.

    import java.sql.Connection;

    import java.sql.DriverManager;

    import java.sql.SQLException;

    import javax.naming.InitialContext;

    import javax.naming.NamingException;

    import javax.sql.DataSource;

    public class ORCLConnection {

        public Connection getConnection(){

            InitialContext ctx;

            try {

                ctx = new InitialContext();

                DataSource ds = (DataSource)ctx.lookup("jdbc/mydb");

                Connection connection = ds.getConnection();

                return connection;

            } catch (NamingException e) {

               e.printStackTrace();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

    }

    Thanks

    Sanjeev

  • 3032683
    3032683 Member Posts: 25
    edited Jun 27, 2017 1:41AM

    Hi Sanjeev,

    Thank you.I tried above code, But I have received below mentioned issue

    javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial

    so I have added

    Hashtable env = new Hashtable();

        env.put( Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory" );

        env.put(Context.PROVIDER_URL, "t3://<actualhost>:7002");

        env.put(Context.SECURITY_PRINCIPAL, "weblogic");

        env.put(Context.SECURITY_CREDENTIALS,"welcome1");

    Context context=new InitialContext( env );

    But after this, still I am getting issue like as shown below

    javax.naming.CommunicationException: [Login failed for an unknown reason:   

    Root exception is weblogic.socket.UnrecoverableConnectException: [Login failed for an unknown reason

    t weblogic.jndi.internal.ExceptionTranslator.toNamingException(ExceptionTranslator.java:44)

      at weblogic.jndi.WLInitialContextFactoryDelegate.toNamingException(WLInitialContextFactoryDelegate.java:889)

    is this issue because of port? should we use 7001 instead of 7002? Please suggest.

    Thanks

    Kotresh

  • SanjeevChauhan
    SanjeevChauhan Member Posts: 1,554 Gold Trophy
    edited Jun 27, 2017 2:18AM Answer ✓

    How are you testing your code?

    My assumption is

    1. you have jndi connection created on weblogic.

    2. You are deploying your code on same weblogic and then you are testing it.

    I suspect you are just trying to run your java file. In that case code is not deployed on weblogic and it runs as standalone java program. It may not be able to use jndi created in weblogic server.

    You may want to use hard coded details in that case.

        public Connection getConnection(){

            try {           

                DriverManager.registerDriver (new oracle.jdbc.OracleDriver());

                String connectionUrl = "jdbc:oracle:thin:<user>/<password>@//&lt;host>:<port>/<sid-or-service>";

                OracleConnection conn = (OracleConnection)DriverManager.getConnection(connectionUrl);

                conn.setAutoCommit(false);

              return conn;

            } catch (Exception e) {

               e.printStackTrace();

               DiagnosticUtil.printStackTrace(e);

               throw new RuntimeException(e.getMessage());

            }

        }

    My suggestion would be to test standalone java you can use above method. The moment you decide to upload war/ear which needs connection inside use my previous post method.

    Thanks

    Sanjeev

  • 3032683
    3032683 Member Posts: 25
    edited Jun 27, 2017 2:58AM

    Thanks Sanjeev.

This discussion has been closed.