Use SQLJ for Enterprise Java Bean (EJB) Bean Managed Persistence (BMP) entity beans, session beans, and servlets

This article describes how JDBC applications, comprised of Bean Managed Persistence (BMP) beans or servlets, are converted to SQLJ applications. It also describes how the SQLJ applications are then deployed in WebSphere Application Server.

Follow these steps precisely and in the right order to ensure a correct conversion:

  1. Create a backup copy of your .java file.For example if your file is called MyServlet.java, copy MyServlet.java to MyServlet.java.bkup.

  2. Rename your .java file to to a file name with a .sqlj extension. For example, if your application is a servlet named MyServlet.java, rename MyServlet.java to MyServlet.sqlj.

    Now when you run the sqlj tool in the next step, the .java file that it creates will have the same name as your old .java file, providing you with a seamless transition to the SQLJ technology.

  3. Edit the .sqlj file to convert the JDBC syntax to SQLJ syntax. When using SQLJ in WAS, if you want WAS connection management to function properly, specify correct connection contexts.

    For example, convert the following JDBC operation

                Connection con = dataSource.getConnection();
                Statement stmt = con.createStatement();
                stmt.execute("INSERT INTO users VALUES (1, 'user1')");
                con.commit();
    
    
    to the following SQLJ

                 // At the top of the file and just below the import statements, define Connection_Context
                 #sql context Connection_context;
                  .
                  .
                 Connection con = dataSource.getConnection();
                  .
                  .
                 Connection_context ctx1 = new Connection_context(con);
                  .
                  .
                 #sql [ctx1] {INSERT INTO users VALUES (1, 'user1')};
                  .
                  . 
                 con.commit();
                 ctx1.close();
    
    

  4. Run the DB2 sqlj translator.This tool creates a .java version of your .sqlj file as well as a .ser profile that is used later in the processing. Refer to the DB2 documentation for more information on this tool.

  5. Package your EJB jar and deploy it in the usual manner.

  6. After the EJB files are deployed, run the db2sqljcustomize tool to customize your .ser file.One serialized profile exists for each EJB .jar file. You can find the profile in the EJB .jar file. One example of a serialized profile file name is MyBMPBeanProfile.ser.

    When you run the DB2 SQLJ customizer against the serialized profiles, you create static SQL in the database, which is used at runtime. The customization phase creates four database packages that contain static SQL, one for each isolation level.

  7. Configure your database.

  8. Update your EJB jar with the .ser file.

  9. Package your EJB jar and servlets, along with .ser files, into an .ear file.

  10. Install the application in the usual manner.

 

See Also

Embedded SQLJ support
Data access from an enterprise entity bean
Exceptions pertaining to data access
Developing data access applications
Data access bean types