I am using the following pattern for my service EJB3 stateless beans. Basically, each database table is exposed through a DAO / facade type class (itself also a stateless EJB3 bean).
My hypothetical AService bean is then injected with the facades of the various database tables it needs to use. However, certain more complex logic or logic is not provided in the facades (which are used exlusively for CRUD-type operations on one table). For this reason, I am also directly injecting the DataSource so I can use JDBC for the more complex queries.
Are there any glaring or subtle anti-patterns / caveats in my approach?
@Stateless
@Local (IAService.ILocal.class)
@Remote(IAService.IRemote.class)
public class AService implements IAService.ILocal, IAService.IRemote{
@PersistenceContext(unitName = "cashflowPU")
private EntityManager em;
// inject CRUD facades for straightforward operations
@EJB
private ITableAFacade.ILocal tgAFacade;
...
// inject the data source for non-straightforward operations that require JDBC
@Resource(mappedName="java:/cashflow") DataSource dataSource;
public BigDecimal methodThatNeedsComplexQuery (...params) throws SQLException {
Connection conn = null;
BigDecimal retValue = null;
try {
conn = dataSource.getConnection();
PreparedStatement pstm = ...
ResultSet rs = pstm.executeQuery(); ...
while (rs.next()) ...
} finally {
conn.close();
return retValue;
}
}
}