1. Outside of a Transaction : 將 entity query 的程式段落, 不要包涵在transaction 中. 但是對於 application-managed or extends entity managers 就不是用了.
2. 將Query 封裝在一個 stateless session facade , 在method 宣告 NOT_SUPPORTED. 只要執行到這一個method code, 任何 transaction 都會被 suspended. 已達到read-only 最佳化.
@Stateless
public class QueryServiceBean implements QueryService {
@PersistenceContext(unitName="EmployeeService")
EntityManager em;
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public List<Department> findAllDepartmentsDetached() {
return em.createQuery("SELECT d FROM Department d",
Department.class)
.getResultList();
}
// ...
}
沒有留言:
張貼留言