Thursday, October 10, 2013

Try out Java EE @DataSourceDefinition with DB2 on GlassFish 4

Came back from Java One 2013, learned some tricks from Adam Bien, I decided to give @DataSourceDefinition a try on GlassFish 4 with DB2 database, after a little bit bumps, I got it work:

  1. Copy your DB2 JDBC driver files(db2jcc4.jar and db2jcc_license_cu.jar) to glassfish/lib direcotry.
  2. Setup a singleton EJB to define your DataSourceDefinition:
  3. @DataSourceDefinition(
     className="com.ibm.db2.jcc.DB2DataSource",
     serverName="host_name",
     name="java:global/jdbc/APP",
     databaseName="database_name",
     portNumber=db_port,
     user="username",
     password="passowrd",
     properties={"driverType=4"}
    )
    @Singleton
    public class DataSourceConfiguration {
    }
    
  4. Add the JNDI reference in your persistence.xml:
  5. java:global/jdbc/APP
    
  6. In your Rest resource file
  7. ...
    @PersistenceContext
    EntityManager em;
    ...
    @GET
    @Produces("application/xml")
    public String getXml() {
        Query q = em.createQuery("select c from Customer c");
        List customers = q.getResultList();
        return "" + customers.get(1).getId() + "";
    }
    
    
    
There are a few catches I ran into:
  • Error:
  • Failure in loading native library db2jcct2, java.lang.UnsatisfiedLinkError
    
    You need to add the vendor specific properties: driverType=4 to force it use type 4 JDBC driver.

No comments:

Full Guide for using Bitnami Prometheus Operator Helm Chart with Additional Scrape Configuration

"The Prometheus Operator for Kubernetes provides easy monitoring definitions for Kubernetes services and deployment and management of...