Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

EDT:Resource Binding Databases

Revision as of 17:50, 7 February 2012 by Margolis.us.ibm.com (Talk | contribs)

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 in the EGL deployment descriptor, you specify a set of details for use at application run time. In some cases, you also specify a set of server configuration details that are provided to the EGL deployer.

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).  and now, please skip to the next section. ]

AddaSQLDatabaseBinding7.png


To specify where the binding details are stored, select one of the first three options:

  • To use connection details that are or will be specified in an Eclipse connection profile, select the first option. Reference an existing connection profile or press New to define one.
  • To specify connection details directly in the binding definition, select the second option instead.
  • To use an JNDI data source that is or will be defined in the application server, select the third option. In this case, the details that you specify are the only ones on this page.

If code is running on an application server that is compliant with Java Enterprise Edition, JNDI allows for fast database access across multiple users.

If you intend to deploy your code to the Apache Tomcat server, you can configure a JNDI entry:

  • Define most of the binding details by specifying the first or second option.
  • Also select the fourth option. The JNDI name and other details that you specify here are associated with the details from the first or second option. The information is packaged during EGL deployment. 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 select the first 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 re-deploy the application.

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:

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

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:

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

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? = 
   SysLib.getResource("binding:MyBinding");            // .8 syntax

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

myOtherJNDIDataSource SQLJNDIDataSource? = 
   SysLib.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

Back to the top