EGL Development User Group

EGL Development User Group

EGL Development User Group

The EGL Development User Group is dedicated to sharing news, knowledge, and insights regarding the EGL language and Business Developer product. Consisting of IBMers, HCL, and users, this community collaborates to advance the EGL ecosystem.

 View Only
Expand all | Collapse all

easy way to access two databases in a project

  • 1.  easy way to access two databases in a project

    Posted 04/21/15 02:50 AM

    Hello, 

    I want to access two databases in Rich UI application with services. One database works fine

    but for the second I have only used imports and I can't do the connection.

    What exactly are the steps to do this?  

    Thanks

    michaeldefox


  • 2.  Re: easy way to access two databases in a project

    Posted 04/21/15 02:54 AM

    I don't know if it is possible.

    Creating two service projects seems like the easiest solution, but could have performance drawbacks if you require 1 function to access both databases.

    Commitment control wil be difficult as well...

    Bram_Callewaert


  • 3.  Re: easy way to access two databases in a project

    Posted 04/21/15 07:26 AM

    Is it possible to give me an example or more steps to understand the connection?

    Thanks

    michaeldefox


  • 4.  Re: easy way to access two databases in a project

    Posted 04/21/15 07:28 AM

    How many web service projects do you have at the moment? 

     

    Bram_Callewaert


  • 5.  Re: easy way to access two databases in a project

    Posted 04/21/15 08:34 AM

    It is possible in one service project...

    You have to replace the jdbc default database in the web.xml for your Service which connects to an other database.

     

        <env-entry>
            <env-entry-name>vgj.jdbc.default.database.DB2service</env-entry-name>
            <env-entry-type>java.lang.String</env-entry-type>
            <env-entry-value>jdbc/XXXXXX</env-entry-value>
        </env-entry>
        <env-entry>
            <env-entry-name>vgj.jdbc.default.database.MSSQLService</env-entry-name>
            <env-entry-type>java.lang.String</env-entry-type>
            <env-entry-value>jdbc/YYYYYY</env-entry-value>
        </env-entry>

     

    With my solution you have to use the same User and Password to connect to both Databases.

     

     

    Second solution :

    You could use an external Java program...

     

    Kind Regards

    Marcel-D


  • 6.  Re: easy way to access two databases in a project

    Posted 04/21/15 08:58 AM

    Ok I will check this, but web.xml changes after deployment . If I change it and then deploy my project it's going to take the default database. Is that right? Do I have to change something in the build descriptor?

     

    THanks

    michaeldefox


  • 7.  Re: easy way to access two databases in a project

    Posted 04/21/15 09:07 AM

    Yes you are right.

    I don't know how to set this in the build descriptor.

    For me it's no problem because i don't change something in this project...

    Marcel-D


  • 8.  Re: easy way to access two databases in a project

    Posted 04/21/15 09:48 AM

    Not sure if I this will be a clear answer.

    What is placed in the web.xml is based on the values specified in the build descriptor.  For example, the sqlJNDIName is what supplies the value for the vgj.vgj.jdbc.default.database.<servicename> property in the web.xml.

    So, your options:

    - Use different build descriptors, one set for database 1 and one set for database 2.    Then use the appropriate build descriptor for the service to set the right jndi connection info.   You can set the default build descriptor at a file, package, or project level...so assuming the packages are different, you can set the correct one appropriately.   (see note 1 below for more info)

    - Manually set the web.xml after generation.  You just have to be careful as subsequent generations will overwrite the changes.    You can control the overwrite by using genProperties=NONE after you have them set the way you want.

    On deployment, the values will not change unless you regenerate and somehow update it to the wrong values.  Whatever is in the web.xml gets move along when you export/deploy the services.

    Finally, remember that the values in the web.xml control the "implicit" connection.   You can always override this by coding your own sqllib.connect within the code itself.  (see NOTE 2)

    Mark

    NOTE 1:  If you are concerned with having too many build descriptors to use, then look at using the "nextBuildDescriptor" build option.  So for example, you can do the following:

    MSSQLBuilddesc - sqlJNDIName=jdbc/XXXXXX, nextBuildDescriptor=commondescriptors

    DB2Builddesc - sqlJNDINAME=jdbc/YYYYYY, nextBuildDescriptor=commondescriptors

    Then the only unique item is the JNDI name.   All other are set in the commonDescriptors.

    NOTE 2:  if you use Sqllib.connect, you could always set your own property in the web.xml, say something like "mydb".  Then you can use the syslib.getProperty to read this property and use the value to populate the sqllib.connect values (or any other deployment value you want to use). .

     

    markevans


  • 9.  Re: easy way to access two databases in a project

    Posted 04/22/15 02:21 AM

    I have created one build descriptor in the service and one in rich Ui project , by right clicking the default descriptor and click "Add part".

    Now this build descriptor is under the default descriptor along with the MyProjectJavaBuildOptions. Is it the right way or I have to build a new build  descriptor out of the default? 

    And what is next? How do the handlers and the services connect to a specific build descriptor ?

    As you mentioned before  at  the web.xml, I should see something  like this: 

    <env-entry>
    <env-entry-name>vgj.jdbc.default.database.Service1</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>jdbc/test_sql</env-entry-value>
    </env-entry>
    <env-entry>
    <env-entry-name>vgj.jdbc.default.database.Service2</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>jdbc/test_db2</env-entry-value>
    </env-entry>

     

    michaeldefox


  • 10.  Re: easy way to access two databases in a project

    Posted 04/22/15 05:42 AM

    Hi,

    i don't think you have to create a build descriptor manually?!

    In my project i have two services and each service has a "vgj.jdbc.default.database" in the web.xml of the deployed project..

    (take a look at the screenshot.)

     

    In the test environment i think you have to put something like this in front of your SQL Statement.

    I think only in the service which connects to the '2nd Database' (which is not the default in your user preferences - take a look at the second screenshot)

             dbConnectionString string = "jdbc:sqlserver://SERVER:1433;databaseName=XXX";        try       SQLLib.connect(dbConnectionString, "USER", "PW");    onException(ex AnyException)        syslib.writeStdout(ex.message);    end

     

    Sorry but it's a long time ago i did this...

     

    Marcel-D


  • 11.  Re: easy way to access two databases in a project

    Posted 04/22/15 09:04 AM

    Hi,

    Marcel-D is correct in that you do not have to create new build descriptors.   The properties in the web.xml will be updated with whatever information is provided in the build descriptors.  By this, I mean it will always create a vgj.jdbc.default.database.<servicename>=<sqlJNDIName> in the web.xml.

    Where <servicename> is the service being generated

               <sqlJNDIname> is the datasource JNDI name for the connection.

     

    What I was suggesting was different build descriptors so no manual changes were needed.

    To answer your questions,

    a.) If you create a new part, then you have to set the values as well.   Instead of "adding" a part, it might be easier to replicate your "javaBuild" options and then modify the appropriate fields.

    b.) You can specify a default build descriptor at the file (.egl), the package, the EGLSource, the Project, and the workspace level.   The search order is this order as well.   So, as an example,

           - My Project

                - EGLSource

                    - SQLPackage                  <--- default build descriptor set here for SQL Db

                    - DB2Package                  <---- Default build descriptor set here for DB2 Db

                    - RUI Package  (if in same project)   <----- default build descriptor set to JavaScript Build options

    The combinations are almost endless...just remember you can set the default to as low a level as you want for generation time. 

    For Debug, Marcel-D is correct but it depends on how you are doing "debug".  If you are debugging while running on an application server, you must use a datasource name.  If you are debugging outside an application server (e.g. debug as->EGL Rich UI), then you need to connect using a connection URL as Marcel-D stated.  

    hope this helps.

    Mark

    markevans


  • 12.  Re: easy way to access two databases in a project

    Posted 04/28/15 04:11 AM

    I tried this but nothing's changed.

    service Example

       dbConnectionString string = "jdbc:sqlserver://SERVER:1433;databaseName=XXX"              
    function blabla()   
    try
           SQLLib.connect(dbConnectionString, "sqlJNDIName", "PW");
           get SearchRec  with 
         #sql
             .............. 
        end

    end

    end

     

    or I used sqlID instead of  sqlJNDIName it didn't do any difference.

     

    michaeldefox


  • 13.  Re: easy way to access two databases in a project

    Posted 04/29/15 03:24 AM

    Hi Michael,

    i think you have to use the database user

     SQLLib.connect(dbConnectionString, "DBUSER", "DBUSER_PW");

    Do you get an error?

     

    Kind regards

    Marcel-D


  • 14.  Re: easy way to access two databases in a project

    Posted 04/29/15 04:14 AM

    Hello again,

    I get this error now:

    EGL0505E:EGL0505E Cannot connect to jdbc:sqlserver://SERVER:1433;databaseName=XXX: Name comp/env/jdbc:sqlserver: not found in context "java:".
    EGL0002I The error occurred in YService processing the Y function.

    I tried to connect to this database using Database Development from eclipse and connection succeeded. 

    Is there any solution to access data from my egl code?

    michaeldefox


  • 15.  Re: easy way to access two databases in a project

    Posted 04/29/15 06:28 AM

    Do you want connect to a MS SQL Database?

    Do you have the correct driver?

    Marcel-D


  • 16.  Re: easy way to access two databases in a project

    Posted 04/29/15 06:34 AM

    Yes exactly this.

    How can I configure the correct driver? 

    michaeldefox


  • 17.  Re: easy way to access two databases in a project

    Posted 04/29/15 06:48 AM

    I've the sqljdbc4 driver in the Java Buildpath...

    Take a look at the screenshot.

    Marcel-D

    Attachments



  • 18.  Re: easy way to access two databases in a project

    Posted 04/29/15 08:00 AM

    In both service and richui project ?

    And now i get these strange errors. Take a look at the screenshot.

     

    michaeldefox

    Attachments



  • 19.  Re: easy way to access two databases in a project

    Posted 04/29/15 08:48 AM

    I think only in the service project...

     

    Marcel-D


  • 20.  Re: easy way to access two databases in a project

    Posted 04/29/15 08:56 AM

    I get this error again and after the driver:

    EGL0505E:EGL0505E Cannot connect to jdbc:sqlserver://SERVER:1433;databaseName=XXX: Name comp/env/jdbc:sqlserver: not found in context "java:".
    EGL0002I The error occurred in YService processing the Y function.

     

    michaeldefox


  • 21.  Re: easy way to access two databases in a project

    Posted 04/29/15 09:06 AM

    Hi,

    Is the service running on an application server when you get this message (i.e. Tomcat or WAS and in debug or run mode).

    I believe this message comes out because the database is being accessed via a data source (i.e. via J2EE standards).

    This means you need to:

    a.) Specify a JNDI name in the sqllib.connect statement instead of a connection URL

    b.) Define a Datasource to your application server (as you would have done for DB2)

    c.) Include the jdbc driver in the CLASSPATH of the application server (if needed).

    FYI...adding a "driver" to the workspace's drivers does not allocate it to the project or to the application server.

    Mark

    markevans