Skip to main content

Junit Tests OpenJPA and Websphere EJB3.0 + DB2 9.0

Junit Tests OpenJPA and Websphere EJB3.0 DB2

For those of us with IBM and their beau software aka tools.

Heres what i had to do to configure the websphere Enterprise container to fire JUnits to persist EJB’s over OpenJPA 1.2.3

You will have to Override persistence.xml file settings to start using JDBC over JTA.

Here’s a snippet from my Abstract Test Case class
Most interesting one is the TransactionMode which is correlated to using

  configOverrides.put("openjpa.TransactionMode", "local");

Allows you to reuse a single persistence unit as Websphere has a bug wherein it would not allow multiple persistence Units to load together. Even worse a separate one for test also fails

Just one more thing
Remember to commit or close connections in the end so:

 public void tearDown() throws Exception {
Complete Abrstract Template
public abstract class AbstractTestCase extends TestCase {

 private Logger logger = LoggerFactory.getLogger(AbstractTestCase.class);
 private EntityManagerFactory emf;

 protected EntityManager em;
 protected EntityTransaction entityTransaction = null;
 public static void setUpBeforeClass() throws Exception {

  * @throws java.lang.Exception
 public static void tearDownAfterClass() throws Exception {

  * @throws java.lang.Exception
  * Finally close connection
  * ensures always
 public void setUp() throws Exception{
  logger.debug("Initialise Setup");
  Properties prop = new Properties();
     try {
               //load a properties file
     } catch (IOException ex) {

  logger.debug("Loading properties file from");

  Map configOverrides = new HashMap();
  configOverrides.put("openjpa.jdbc.Schema", prop.getProperty("jdbc.schemaname"));    
  configOverrides.put("openjpa.ConnectionURL", prop.getProperty("jdbc.connectionurl"));
  configOverrides.put("openjpa.ConnectionDriverName", prop.getProperty("jdbc.connectionDriverName"));
  configOverrides.put("openjpa.ConnectionUserName", prop.getProperty("jdbc.username"));
  configOverrides.put("openjpa.ConnectionPassword", prop.getProperty("jdbc.password"));
  configOverrides.put("openjpa.TransactionMode", "local");
  logger.debug("Finished load of properties file from");

     emf = Persistence.createEntityManagerFactory("Entity", configOverrides);
     em = emf.createEntityManager();
     entityTransaction = em.getTransaction();
  * @throws java.lang.Exception
 public void tearDown() throws Exception {
  logger.debug("Initialise tearDown");
  logger.debug("All connections closed Successfully");


Popular posts from this blog

ActionController::Live rails 4 Nginx passenger and redis

This is the most common setup for rails applications and most straightforward solution for SSE aka server side events.

In order to get SSE to work using redis we just use Ajax to respond to publish events based on json fields being present but you can even send a pure data payload: