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 23:55, 26 January 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:


[ replace, when possible ]

Bind Img8.JPG


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; and
  • 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 deployment establishes 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:

myDataSource SQLDataSource? { @Resource {bindingKey="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:

myJNDIDataSource SQLJNDIDataSource? { { @Resource {bindingKey="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("MyBinding");

myJNDIDataSource SQLDataSource? SysLib.getResource("MyJNDIBinding");

myOtherJNDIDataSource SQLJNDIDataSource? SysLib.getResource("MyOtherJNDIBinding");
 

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"};
ds 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"};
ds 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";
ds SQLJNDIDataSource? = new SQLJNDIDataSource(connectURL);

If security detail is passed to a data source that operates under container-managed security, the result is not determined by the EGL-generated code. For details on what does happen, see the documentation for the data source itself.

For details on the SQLDataSource and SQLJNDIDataSource types, see the help topics under "eglx.persistence.sql package."

Previous:  Service bindings

First:  Resource binding introduction

Back to the top