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

Difference between revisions of "EDT:Resource Binding Databases"

Line 37: Line 37:
  
 
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: <br>  
 
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: <br>  
<pre>myDataSource SQLDataSource? { @Resource {bindingKey="MyDatabaseBinding"} };  
+
<pre>myDataSource SQLDataSource? { @Resource {uri="binding:"MyDatabaseBinding"} };  
 
</pre>  
 
</pre>  
 
That declaration is valid whether the binding is for JNDI or not.&nbsp; 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: <br>  
 
That declaration is valid whether the binding is for JNDI or not.&nbsp; 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: <br>  
 
<pre>myJNDIDataSource SQLJNDIDataSource? {  
 
<pre>myJNDIDataSource SQLJNDIDataSource? {  
   { @Resource {bindingKey="MyDatabaseJNDIBinding"} };
+
   { @Resource {uri="binding:MyDatabaseJNDIBinding"} };
 
</pre>  
 
</pre>  
 
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:&nbsp; if your subsequent logic uses the EGL <code>isa</code> 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.  
 
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:&nbsp; if your subsequent logic uses the EGL <code>isa</code> 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.  
Line 47: Line 47:
 
You can also access SQL database bindings in your logic, as shown here:&nbsp;  
 
You can also access SQL database bindings in your logic, as shown here:&nbsp;  
 
<pre>myDataSource SQLDataSource? =  
 
<pre>myDataSource SQLDataSource? =  
   SysLib.getResource("MyBinding");
+
   SysLib.getResource("binding:MyBinding");
  
 
myJNDIDataSource SQLDataSource? =  
 
myJNDIDataSource SQLDataSource? =  
   SysLib.getResource("MyJNDIBinding");
+
   SysLib.getResource("binding:MyJNDIBinding");
  
 
myOtherJNDIDataSource SQLJNDIDataSource? =  
 
myOtherJNDIDataSource SQLJNDIDataSource? =  
   SysLib.getResource("MyOtherJNDIBinding");
+
   SysLib.getResource("binding:MyOtherJNDIBinding");
 
   
 
   
 
</pre>  
 
</pre>  
Line 74: Line 74:
 
myJNDIDataSource SQLJNDIDataSource? = new SQLJNDIDataSource(connectURL);
 
myJNDIDataSource SQLJNDIDataSource? = new SQLJNDIDataSource(connectURL);
 
</pre>  
 
</pre>  
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.<br>
+
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.<br>  
  
 
= Using the connection variable for additional purposes<br>  =
 
= Using the connection variable for additional purposes<br>  =
Line 83: Line 83:
 
*To set or get the ''autoCommit value'', which indicates whether updates are committed automatically.  
 
*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.<br>  
 
*To set the database schema to use in SQL statements that are issued by your code.<br>  
*To test whether a connection is still in effect. <br>
+
*To test whether a connection is still in effect. <br>  
 
*To access a set of exception records that give runtime warnings. <br>
 
*To access a set of exception records that give runtime warnings. <br>
  

Revision as of 17:37, 30 January 2012

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 {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:

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");

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

myOtherJNDIDataSource SQLJNDIDataSource? = 
   SysLib.getResource("binding: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"};
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