3 Replies Latest reply on Sep 14, 2010 1:22 PM by Jay SenSharma MiddlewareMagic

    how 2 access specific datasource using java code as per user requirement

    715786
      Hi WL users

      we have one specific requirement like below.

      we have 2 databases where one db is for uat & one for training. for each db, one datasource is available in weblogic console.

      is their a way to access either of them using java coding as per user requirement?

      kindly help.

      regards
      vishy
        • 1. Re: how 2 access specific datasource using java code as per user requirement
          Jay SenSharma MiddlewareMagic
          Yes, Possible.

          Please make a Properties file inside <DOMAIN_HOME> and place the JNDI Name of the dataSource which you want to Use

          Example:
          Step1). Make a Properties file "whichDataSource.properties" inside <DOMAIN_HOME>

          nowUseDataSource=UAT_dataSource_JndiName

          Step2). Now write a JSP page inside your Application to read the DataSource Name which you want to actually use currently:

          "index.jsp"

          <%@ page import="java.util.*" %>
          <html>
          <head>
          <title>Hi It's Wroking</title>
          </head>
          <body bgcolor="maroon" text="white">
          <center>

          <%
          InputStream is=new FileInputStream("./a.properties");
          Properties prop=new Properties();
          prop.load(is);
          out.println("DataSource Which you want to use: "+prop.getProperty("nowUseDataSource"));
          %>

          <%
          try{
          String dataSourceJndiName=prop.getProperty("nowUseDataSource");
          InitialContext ic=new InistalContext();
          javax.sql.DataSource ds=(javax.sql.DataSource)ic.lookup(dataSourceJndiName);
          }
          catch(Exception e)
          {
          e.printStackTrace();
          }
          %>

          </center>
          </body>
          </html>
          • 2. Re: how 2 access specific datasource using java code as per user requirement
            715786
            Hi Jay SenSharma

            Thanks for your reply.

            is their any other way to get same functionality?

            Regards
            vishy
            • 3. Re: how 2 access specific datasource using java code as per user requirement
              Jay SenSharma MiddlewareMagic
              Please refer to : http://weblogic-wonders.com/weblogic/2010/05/31/overriding-properties-files-of-ear-applications/

              In this case place any roperties file mentioning your DataSource JNDI name which your application want to Lookup... Then Using above techniqueue reload the fresh Properties file which has Another DataSource JNDI name to lookup.

              It means the JNDI name present in the properties file will decide which DataSource will be looked up by your application code. And any time you can Change this Property file Which is pesent inide your Application using *"AppFileOverrides"* feature of WebLogic.