An SBB can use JDBC to execute SQL statements. It must declare this intent in an extension deployment descriptor: the oc-sbb-jar.xml file (contained in the SBB jar file in the META-INF directory). The <resource-ref> element (which must be inside the <sbb> element of oc-sbb-jar.xml) defines the JDBC datasource it will use.

Sample <resource-ref>

Below is a sample <resource-ref> element defining a JDBC datasource:

<resource-ref>
    <!-- Name under the SBB's java:comp/env tree where this datasource will be bound -->
    <res-ref-name>foo/datasource</res-ref-name>
    <!-- Resource type - must be javax.sql.DataSource -->
    <res-type>javax.sql.DataSource</res-type>
    <!-- Only Container auth supported -->
    <res-auth>Container</res-auth>
    <!-- Only Shareable scope supported -->
    <res-sharing-scope>Shareable</res-sharing-scope>
    <!-- JNDI name of target JDBC resource, relative to Rhino's java:resource tree. -->
    <res-jndi-name>jdbc/myresource</res-jndi-name>
</resource-ref>
Note In the above example, the <res-jndi-name> element has the value jdbc/myresource, which maps to the JDBC resource created in the example.

How an SBB obtains a JDBC connection

An SBB can get a reference to an object that implements the datasource interface using a JNDI lookup. Using that object, the SBB can then obtain a connection to the database. The SBB uses that connection to execute SQL queries and updates.

For example:

import javax.naming.*;
import javax.slee.*;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
...

public abstract class SimpleSbb implements Sbb {
    public void setSbbContext(SbbContext context) {
        try {
            Context myEnv = (Context)new InitialContext().lookup("java:comp/env");
            ds = (DataSource)myEnv.lookup("foo/datasource");
        }
        catch (NamingException e) {
            // JNDI lookup failed
        }
    }

    public void onSimpleEvent(SimpleEvent event, ActivityContextInterface context) {
        Connection conn;
        try {
            conn = ds.getConnection();
        }
        catch (SQLException e) {
            // could not get database connection
        }
        ...
    }
    ...

    private DataSource ds;
    ...
}

SQL programming

When an SBB executes in a transaction and invokes SQL statements, the SLEE controls transaction management of the JDBC connection. This lets the SLEE perform last-resource-commit optimisation.

Invoking JDBC methods which affect transaction management have no effect or undefined semantics when called from an application component isolated by a SLEE transaction. The methods (including any overridden form) that affect transaction management on the java.sql.Connection interface are listed below. These methods should not be invoked by SLEE components:

  • close

  • commit

  • rollback

  • setAutoCommit

  • setIsolationLevel

  • setSavePoint

  • releaseSavePoint

Previous page Next page
Rhino Version 2.7.0