Forum Stats

  • 3,780,523 Users
  • 2,254,406 Discussions
  • 7,879,373 Comments

Discussions

Data source on production application

Abdelrahman ElGiar
Abdelrahman ElGiar Member Posts: 36 Green Ribbon

Hello,

what's the difference between the 2 ways of using data sources on the production?

  1. java:comp/env/jdbc/datasourceName that use included connection of application.
  2. jdbc/datasourceName that use data source defined on WebLogic.

what's the adv and dis of 2 ways?

thanks in advance.

Tagged:

Answers

  • Timo Hahn
    Timo Hahn Senior Principal Technical Consultant - Oracle ACE Director Member, Moderator Posts: 37,765 Red Diamond

    The first solution is only meant for local testing. It creates a data source on the fly. This is something you normally are not allowed to do on a production server. There only the system admin can create data sources. As a developer, you normally don't even know the needed password for the connection to the production DB. And it'S harder to switch to different db settings like different db servers.

    The second solution offers this flexibility as you only talk to a data source already defined in the WLS. You don't need to know the details behind the connection. Your application can be deployed to every WLS as long as the defined data source has been created before. If not you get a deployment error.

    In the real world, when you create an application you should only use the second solution. This allows you to create or use the same procedure to deploy the application to all WLS instances in the product lifetime, like dev WLS, QA WLS and production WLS without the need to change anything.


    Timo

    Abdelrahman ElGiar
  • dvohra21
    dvohra21 Member Posts: 14,309 Gold Crown

    These are not two different options for lookup. The prefix is needed for local application context.


    Applications lookup a data source on the JNDI tree or in the local application context (java:comp/env), depending on how you configure and deploy the object, and then request a database connection. 


    javax.sql.DataSource dataSource = (javax.sql.DataSource) context.lookup("java:comp/env/jdbc/myDataSource");

    Abdelrahman ElGiar
  • dvohra21
    dvohra21 Member Posts: 14,309 Gold Crown

    In the standard Java EE descriptor files packaged with your application modules, such as ejb-jar.xml for an EJB, you must add resource-ref-name references to specify the JNDI name of the data source as used in the application. For example:

    <resource-ref>
      <res-ref-name>my-data-source</res-ref-name> 
      <res-type>javax.sql.DataSource</res-type> 
      <res-auth>Container</res-auth> 
    </resource-ref>
    
    

    In this example, my-data-source is the data source name as used in the application module. Your application would look up the data source with the following code:

    javax.sql.DataSource ds = 
        (javax.sql.DataSource) ctx.lookup("java:comp/env/my-data-source");
    

    To get a connection from JDBC module packaged with an enterprise application, you look up the data source defined in the JDBC module in the local environment (java:comp/env) or on the JNDI tree and then request a connection from the data source or Multi Data Source. For example:

    javax.sql.DataSource ds = 
        (javax.sql.DataSource) ctx.lookup("java:comp/env/my-data-source");
    java.sql.Connection conn = ds.getConnection();
    


    Abdelrahman ElGiar
  • Abdelrahman ElGiar
    Abdelrahman ElGiar Member Posts: 36 Green Ribbon

    aI deployed adf I deployed ADF application on production and didn't remove my db connection and in data source name inside application module used this java:comp/env/jdbc/datasourceName not the jdbc/datasourceName

    so my application read and write from included db connection not from the connection on the WebLogic datasource

    is there's a problem with that?

  • Timo Hahn
    Timo Hahn Senior Principal Technical Consultant - Oracle ACE Director Member, Moderator Posts: 37,765 Red Diamond

    This depends on your admin. As I said, normally you are not allowed to create your own DB connections. DB connections are defined by the admins and DBAs. They will not tell you the needed credentials. You can't use your own DB connection.

    They only tell you the name of the connection. This you should use in your app.


    Timo

    Abdelrahman ElGiar