1 2 Previous Next 16 Replies Latest reply: Sep 28, 2011 10:42 AM by Ravi Jegga RSS

    Use DataSource of weblogic in a standalone Java file

    707797
      Hi,

      We have a requirement to run a java file at a scheduled time in a day using cron scheduler on our linux server.

      We need to fetch data from the database & perform some business logic in this standalone JAVA file.

      Our application has an EAR which is deployed on Weblogic 10.3 server & in our application, we are utilizing the datasource created in that domain using Hibernate.

      Now, can we create a standealone Java file & use exisitng datasource (without Hibernate) instead of legacy JDBC code to connect to DB.

      Also, do we need to keep this JAVA file a part of this EAR, WAR or can we put the class file in anylocation outside this EAR & then utilize datasource feature.

      Please help on the same in implementation.

      Thanks,

      Uttam
        • 1. Re: Use DataSource of weblogic in a standalone Java file
          René van Wijk
          An example to retrieve resources from WebLogic is provided here: http://middlewaremagic.com/weblogic/?p=6193

          You can run the program as a normal Java client.
          • 2. Re: Use DataSource of weblogic in a standalone Java file
            Ravi Jegga
            Hi Uttam
            In addition to what Rene already mentioned, if you get any error like Remote JDBC disabled etc, make sure to edit your setDomainEnv.cmd (.sh) and add a flag shown below. In later vesions of Weblogic, weblogic is allowing any JDBC connections using JNDI lookup only within the same JVM. But if you want to access totally from a different JVM, then you had to set this flag. Different JVM means, you startWeblogic and this runs in one JVM. I assume you created a some DataSources. Now within the same domain if you have like EJBs, JSPs deployed they all can access this DataSource. But if you have a normal java client like HelloWorld client with main program and when you run this HelloWorld its run in a different new JVM. Thats where the below flag has to be set.

            set WLS_JDBC_REMOTE_ENABLED=-Dweblogic.jdbc.remoteEnabled=true

            And yes, try to use old native way to get connection by passing database url, username, password etc. Instead configure connection pool, datasource and use DataSource to access. This is more standard way.

            Thanks
            Ravi Jegga
            • 3. Re: Use DataSource of weblogic in a standalone Java file
              707797
              Thanks a lot Ravi.

              I'll use the below approach as suggested

              1) I'll create a standalone JAVA file using DataSource code.
              2) Will keep this file in the WAR file deployed in the domain where datasources configured.

              In this case, I don't need to set the flag as you suggested. Is this understanding correct?
              • 4. Re: Use DataSource of weblogic in a standalone Java file
                707797
                Hi Ravi,

                I did create Datasource domain & put wlclient.jar in my application classpath (Add jar in Java Build path of application), but, when I ran application, its giving below error

                Exception in thread "Main Thread" java.lang.NoClassDefFoundError: weblogic/kernel/KernelStatus
                     at weblogic.jndi.Environment.<clinit>(Environment.java:78)
                     at weblogic.jndi.WLInitialContextFactory.getInitialContext(WLInitialContextFactory.java:117)
                     at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667)
                     at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
                     at javax.naming.InitialContext.init(InitialContext.java:223)
                     at javax.naming.InitialContext.<init>(InitialContext.java:198)
                     at TestDataSource.main(TestDataSource.java:37)

                Also, I'm putting code here as I there is no provision to attach the file here. Please let me know whats wrong here.

                Also, I've given the name of datasource as testDataSource through Admin console of the domain. Do we need to prefix the name with jdbc/ in the code or without that also it works?

                import java.sql.Connection;
                import java.sql.ResultSet;
                import java.sql.SQLException;
                import java.sql.Statement;
                import java.util.Hashtable;

                import javax.naming.Context;
                import javax.naming.InitialContext;
                import javax.naming.NamingException;
                import javax.sql.DataSource;


                public class TestDataSource {
                     
                     
                     
                     public static void main(String[] args)
                     {
                          DataSource ds=null;
                          Connection conn=null;
                          Statement stmt=null;
                          ResultSet rs=null;
                          Context ctx=null;
                          
                          
                          

                          
                          try
                          {
                               Hashtable ht = new Hashtable();
                               ht.put(Context.INITIAL_CONTEXT_FACTORY,
                               "weblogic.jndi.WLInitialContextFactory");
                               ht.put(Context.PROVIDER_URL,
                               "t3://172.19.244.180:7001");
                               System.out.println("HERE");
                               ctx=new InitialContext(ht);
                               ds=(DataSource)ctx.lookup("jdbc/testDataSource");
                               
                               System.out.println("HERE AFER CONTEXT CREATION");
                               
                               conn=ds.getConnection();
                               
                               stmt=conn.createStatement();
                               
                               rs=stmt.executeQuery("select distinct(circle) from AIRCEL_STORE_FINDER order by 1 ");
                               
                               while (rs.next())
                               {
                                    System.out.println("circle name "+rs.getString(1));
                               }
                          }catch (Exception e) {
                               System.out.println("Error in Main "+e.toString());
                               e.printStackTrace();
                          }
                          
                          finally{
                               
                               try{
                               
                               if(rs!=null)
                                    rs.close();
                                    
                               if(stmt!=null)
                                    stmt.close();
                               
                               if(conn!=null)
                                    conn.close();
                               
                               if(ds!=null)
                                    ds=null;
                               
                               if(ctx!=null)
                                    ctx.close();
                               }catch (SQLException e) {
                                    System.out.println("Error in SQLException Finally "+e.toString());
                               }
                               catch (NamingException e) {
                                    System.out.println("Error in NamingException Finally "+e.toString());
                               }
                          }
                     }

                }
                • 5. Re: Use DataSource of weblogic in a standalone Java file
                  Ravi Jegga
                  Hi Uttam
                  Instead of wlclient.jar you need to generate something called wlfullclient.jar and add that in the classpath. Here is the link for more details. This is a required step.

                  http://download.oracle.com/docs/cd/E12840_01/wls/docs103/client/jarbuilder.html (To create wlfullclient.jar)
                  http://download.oracle.com/docs/cd/E12840_01/wls/docs103/client/t3.html
                  http://download.oracle.com/docs/cd/E12840_01/wls/docs103/client/basics.html#wp1069994

                  1. In AdminConsole, the name of datasource you mention should match exactly when you do jndi lookup. Say in AdminConsole, you give this as "jdbc/MyTestDS", then in the java code you should use the same full name. The prefix jdbc is not required but its a standard like the jndi lookup is a DataSource like that. Point is, the names should match exactly. There is nothing like prefix required etc.

                  Thanks
                  Ravi Jegga
                  • 6. Re: Use DataSource of weblogic in a standalone Java file
                    707797
                    Hi Ravi,

                    I did create wlfullclient.jar & included in the classpath.

                    Re-build entire application, still getting the same exception. The exception is raised immediately before context creation i.e. ctx=new InitialContext().
                    • 7. Re: Use DataSource of weblogic in a standalone Java file
                      707797
                      Hi Ravi,

                      with the help of the below article, I got rid of the exception weblogic kernel exception,

                      http://www.javamonamour.org/2010/06/javalangnoclassdeffounderror.html

                      But, now the lookup is not happening & throwing below error

                      Error in NamingException javax.naming.NameNotFoundException: Exception in lookup.: `testDataSource' could not be found. [Root exception is weblogic.corba.cos.naming.NamingContextAnyPackage.NotFound: IDL:weblogic/corba/cos/naming/NamingContextAny/NotFound:1.0]

                      Could you please help?
                      • 8. Re: Use DataSource of weblogic in a standalone Java file
                        Ravi Jegga
                        Hi Uttam
                        1. In Weblogic Console, when you create the DataSource, what is the exact name of JNDI name you gave. AND, when you create, please click on next button in the each screen till the end and make sure you TARGET your datasource to the AdminServer and Cluster (if you have any Cluster). Most of the times, users just click on Finish button, but the target section is in the last section. So make sure to target this DataSource.

                        2. Test your datasource in weblogic console from Monitoring tab like that.

                        3. Paste your JavaCode that looks up the above DataSource. As mentioned earlier, the datasource name should match exactly the way you defined in weblogic console.

                        4. Finally, if you generated wlfullclient.jar, that JAR does include your missing class weblogic.kernel.KernelStatus. I verified the JAR on my side and I could see that class file. So if you include wlfullclient.jar in classpath, that is just sufficient. There is no need to include wlclient.jar.

                        Thanks
                        Ravi Jegga
                        • 9. Re: Use DataSource of weblogic in a standalone Java file
                          707797
                          Hi Ravi,

                          Please find below my comments


                          1. I used the name testDataSource. I did deploy it (target) on AdminServer. Its a standalone server, not a cluster.

                          2. I tested it & working fine.

                          3. Java code is above as mentioned.

                          4. I didn't check for weblogic.kernel.KernelStatus, I'll get back to you on this. This jar (wlfullclient.jar is in classpath)

                          Thanks
                          Uttam
                          • 10. Re: Use DataSource of weblogic in a standalone Java file
                            707797
                            One correction - I used testDataSource on Admin Console & used same name in lookup (not the one mentioned above - jdbc/testDataSource ).
                            • 11. Re: Use DataSource of weblogic in a standalone Java file
                              707797
                              Hi Ravi,

                              Thanks, I removed wlclient.jar from classpath & kept wlfullclient.jar & it worked fine.

                              Could you please help me understand the logic behind the same?

                              Regards,

                              Uttam
                              • 12. Re: Use DataSource of weblogic in a standalone Java file
                                Ravi Jegga
                                Hi Uttam
                                I am glad finally you got this working. What you are doing is very basic thing provided if you added this wlfullclient.jar in the initial steps itself. Adding wlclient.jar will not help. This wlclient.jar do NOT have all the dependent files that are referenced internally by other files like in import commands. But when you generate wlfullclient.jar, it does have all the required class files and infact you can see the size of this jar also much higher. Refer the 3 links that I sent earlier and it has details like why and when to use this wlfullclient.

                                I guess, you can mark this thread answered :) since the issue is resolved.

                                Thanks
                                Ravi Jegga
                                • 13. Re: Use DataSource of weblogic in a standalone Java file
                                  707797
                                  Thanks a lot Ravi on this. Its really good. Appreciate that.

                                  Now, I'm struggling with running this standalone Java file on server (Solaris) through java command.

                                  The reason being, this is bundled in WAR file & application EAR is deployed which is using weblogic classpath.

                                  When I do echo $CLASSPATH, its not showing anything in results.

                                  How do I execute this file from any path which can reference classpath of weblogic.

                                  java & javac commands are executable.
                                  • 14. Re: Use DataSource of weblogic in a standalone Java file
                                    707797
                                    Hi Ravi,

                                    Could you please suggest something on this?

                                    Regards,

                                    Uttam
                                    1 2 Previous Next