Jump to: navigation, search

EDT:Resource Binding Databases

Resource Binding Introduction
If the purpose of a resource binding is to connect to a relational database, the definition is called an SQL database binding.

Defining an SQL database binding in the EGL deployment descriptor

When you define an SQL database binding, you specify connection details that are used at development time. In addition, you can identify a JNDI name:

  • If you are deploying to the Apache Tomcat Server, EGL deployment uses both the JNDI name and the connection details to create the JNDI <resource-ref> entry in the WEB-INF\web.xml and the <resource> entry in the META-INF\context.xml file that will be used at run time. The only additional thing you need to do is make sure the derbyclient jar is copied to the Tomcat lib directory.

If the same-name JNDI entry is already in place, your connection details supplement the existing entry. The fields that are available in the EGL deployment descriptor override the equivalent attributes in the existing entry. The override occurs even if you do not specify a value at all. However, if a JNDI attribute is not represented, the JNDI attribute in the existing entry is unaffected.

  • If you are deploying to IBM WebSphere Application Server, the connection details you specify are not used to configure the server, but the name will be used at run time. In this case, you need to configure the data source at the Administrative Console of that server.
  • If you are deploying to another server, the connection details you specify are not used to configure the server, but the name will be used at run time. For details on the configuring the data source, consult the server documentation.


Here is the overall process:

    1. In an EGL project, expand the EGLSource folder and double-click the deployment descriptor, which has the file extension .egldd.
    2. Click the Resource Bindings tab. The Resource Bindings Configuraton page is displayed.
    3. Click Add and, at the Add a Resource Binding page, select SQL Database Binding. The Add an SQL Database Binding page is displayed, as shown here:

      [ this screen shot is for .7 and is under review for .8.  the main point is this:  you can specify what detail is used at deployment time:  either the connection profile (the first option) or details that you type at this screen (the second option). ]

      AddaSQLDatabaseBinding7.png
      After you have created an SQL database binding, you can make additional changes at the following screen, which is new in version .8:

      AddaSQLDatabaseBinding8.png
      At that screen you select one of the first two options to specify where the binding details are stored:
    • To use connection details that are specified in an Eclipse connection profile, select the first option. Reference an existing connection profile or press New to define one.

      If you select this option, any subsequent change to the Eclipse connection profile is available to your code at development and deployment time. After the EGL deployer has packaged the application, though, the changes have no effect on the deployed code unless you redeploy the application.
    • To specify connection details directly in the binding definition, select the second option instead. To configure a JNDI entry, do as follows:
    • Define the connection by specifying the first or second option.
    • Select the third option. In relation to Apache Tomcat, the JNDI name and other details that you specify here are associated with the other details. The information is packaged during EGL deployment.

      By default, the JNDI details result in a container-based authentication, which means that the server stores any user ID and password needed for database access. If you are handling authentication in your code, select Use application-based authentication.

Retrieving an SQL database binding in your code

You enable a future connection to a database by declaring a connection variable. The connection itself occurs when you first run a database-access statement that uses the variable.

One way to enable a future connection is to retrieve an SQL database binding from the EGL deployment descriptor. For example, here is the declaration of a connection variable:

myDataSource SQLDataSource? 
   { @Resource {uri="binding:"MyDatabaseBinding"} };      // .8 syntax

That declaration is valid whether the binding is for JNDI or not.  You can specify a connection variable that is specific to a JNDI data source, but will cause a runtime error if the binding refers to a non-JNDI data source:

myJNDIDataSource SQLJNDIDataSource? { 
   { @Resource {uri="binding:MyDatabaseJNDIBinding"} };   // .8 syntax  

Your code interacts with either variable in the same way, and the use of the SQLDataSource type is sufficient in many cases. Here is an exception:  if your subsequent logic uses the EGL isa operator to test whether a variable is of type SQLDataSource or SQLJNDIDataSource, you must use the SQLJNDIDataSource type for JNDI data sources and must use the SQLDataSource type for others.

You can also access SQL database bindings in your logic, as shown here: 

myDataSource SQLDataSource? = 
   Resources.getResource("binding:MyBinding");               // .8 syntax


myOtherDataSource SQLDataSource? =
   Resources.getResource("binding:file:MyFile#MyBinding2");  // .8 syntax 


myJNDIDataSource SQLDataSource? = 
   Resources.getResource("binding:MyJNDIBinding");           // .8 syntax

myOtherJNDIDataSource SQLJNDIDataSource? = 
   Resources.getResource("binding:MyOtherJNDIBinding");      // .8 syntax
 

Creating an SQL database binding in your code

You can create an SQL database binding in your code, in which case the EGL deployment descriptor is not involved. For example, the following code enables a database connection for a non-JNDI data source:

connectURL string = "jdbc:derby:SomeDB;create=true;";
properties Dictionary{user = "MyID", password = "MyPassword"};
myDataSource SQLDataSource? = new SQLDataSource(connectURL, properties);

Here is equivalent code that is specifically for a JNDI data source:

connectURL string = "jdbc/myDataSource";
properties Dictionary{user = "MyID", password = "MyPassword"};
myJNDIDataSource SQLJNDIDataSource? = 
   new SQLJNDIDataSource(connectURL, properties);

As noted earlier, any connection variable can be based on the SQLDataSource type.

Here is code that enables a JNDI connection in the usual case, when container-managed security is in effect:

connectURL string = "jdbc/myDataSource";
myJNDIDataSource SQLJNDIDataSource? = new SQLJNDIDataSource(connectURL);

For a JNDI connection, if security detail is passed to a data source that operates under container-managed security, the result is not determined by the generated application or by the EGL runtime code. For details on what happens, see the documentation provided by the specific Java DataSource class in use.

Using the connection variable for additional purposes

You can use the connection variable for the following, additional purposes:

  • To set or get the isolation level, which specifies the level of independence
    of one user's database transaction from another user's database transaction.
  • To set or get the autoCommit value, which indicates whether updates are committed automatically.
  • To set the database schema to use in SQL statements that are issued by your code.
  • To test whether a connection is still in effect.
  • To access a set of exception records that give runtime warnings.

For details on these capabilities, see the "SQLDataSource external type" help topic, which is subordinate to "eglx.persistence.sql.package."  The details there apply to both SQLDataSource and SQLJNDIDataSource.

Previous:  Service bindings

First:  Resource binding introduction