Thursday, December 08, 2011

How to get a JDBC connection in EJB 3 running on WebSphere

There are several ways to get a JDBC connection in EJB3 running on WebSphere:
1. JNDI lookup
2. @Resource Injection
2.1 In ibm-ejb-jar-bnd.xml
<session name="OrderRepositoryBean">
<resource-ref name="DFSAdabasDataSource" binding-name="jdbc/DFS_ADABAS" />
2.2 In OrderRespositoryBean
@Resource(name = "DFSAdabasDataSource")
private DataSource ds;
3. NOT WORK!!! @Resource Injection by using mappedName, assuming the JNDI name is : jdbc/DFS_ADABAS
// In OrderRepositoryBean
@Resource(mappedName = "jdbc/DFS_ADABAS")
private DataSource ds;
4. Using OpenJPAEntityManager
@PersistenceContext(unitName = "CACSAMP")
private EntityManager emDFS;
(Connection) ((OpenJPAEntityManager) emDFS.getDelegate()).getConnection();

No comments: