EntityManager object in the Web container, need to do some improvements can be safer. Servlet is not thread-safe, so it is necessary to change the EntityManager object ThreadLocal class.
ThreadLocal is to use a variable for each thread to provide a copy of the value of the variable, so that each thread can independently change their copy, without conflict and a copy of the other threads. From the point of view of the thread, as if each thread in full possession of one of the variables, which would address the problem of the the servlet non-thread-safe.
The following EntityManager object of non-container EJB container-managed manual control transaction. Can be used in a Java SE environment. EJB container management, or Web container can be injected directly into the @ PersistenceContext use JTA Affairs Management in RESOURCE_LOCAL.
- import java.io.Serializable;
- import java.util.Collection;
- import java.util.List;
- /**
- *
- * <p>Title: </p>
- * <p>Description:EasyJWeb ToolsDAO </p>
- * <p>Copyright: Copyright (c) 2006</p>
- * <p>Company: EasyJF</p>
- * @author
- * @version 0.1
- */
- public interface IDAO<T> {
- /**
- *
- *
- * @param obj
- * @return ?true?false
- * @throws IdExistException
- */
- boolean save(T obj);
- /**
- *
- *
- * @param obj
- * @return ?true?false
- */
- boolean update(T obj);
- /**
- *
- *
- * @param obj
- * @return ?true?false
- */
- boolean del(T obj);
- /**
- *
- *
- * @param clz
- * @param id
- * @return ??null
- */
- T get(Class<T> clz, Serializable id);
- /**
- * ???
- *
- * @param clz
- * @param fieldName
- * @param value
- * @return ??null
- */
- T getBy(Class<T> clz, String fieldName, Serializable value);
- /**
- *
- *
- * @param clz
- * @param scope
- * @return
- */
- List<T> query(Class<T> clz, String scope);
- /**
- * ?
- *
- * @param clz
- * @param scope
- * @param paras
- * @return
- */
- List<T> query(Class<T> clz, String scope, Collection paras);
- /**
- * ?beginmax
- *
- * @param clz Java
- * @param scope
- * @param paras
- * @param begin
- * @param max
- * @return
- */
- List<T> query(Class<T> clz, String scope, Collection paras, int begin, int max);
- /**
- * sql?,select count(*) from tableName
- * @param sql sql
- * @return ?null
- */
- Object uniqueResult(String sql);
- /**
- * sql??select count(*) from tableName where filed=?
- * @param sql
- * @param paras
- * @return ?null
- */
- Object uniqueResult(String sql,Collection paras);
- /**
- * sql?
- * @param sql sql
- * @return
- */
- int execute(String sql);
- /**
- * sql?
- * @param sql sql
- * @param paras
- * @return
- */
- int execute(String sql,Collection paras);
- }
***********************************************
***********************************************
- import java.io.Serializable;
- import java.util.Collection;
- import java.util.List;
- import javax.persistence.EntityManager;
- import javax.persistence.EntityManagerFactory;
- import javax.persistence.Query;
- public class EntityMangerDaoSurport<T> implements IDAO<T> {
- private static EntityManagerFactory emf;
- private static ThreadLocal<EntityManager> em;
- public EntityManager getEntityManager() {
- EntityManager manager = em.get();
- if (manager == null) {
- manager = emf.createEntityManager();
- em.set(manager);
- }
- return manager;
- }
- public boolean del(Object obj) {
- getEntityManager().getTransaction().begin();
- if (!getEntityManager().contains(obj))
- obj = getEntityManager().merge(obj);
- getEntityManager().remove(obj);
- getEntityManager().getTransaction().commit();
- return true;
- }
- public int execute(String sql) {
- return execute(sql, null);
- }
- public int execute(String sql, Collection paras) {
- Query query = getEntityManager().createNativeQuery(sql);
- int parameterIndex = 0;
- if (paras != null && paras.size() > 0) {
- for (Object obj : paras) {
- query.setParameter(parameterIndex++, obj);
- }
- }
- return query.executeUpdate();
- }
- public T get(Class<T> clz, Serializable id) {
- return getEntityManager().find(clz, id);
- }
- public T getBy(Class<T> clz, String fieldName, Serializable value) {
- Query query = getEntityManager().createQuery(
- “from “ + clz + ” where fieldName=?”);
- query.setParameter(0, value);
- return (T) query.getSingleResult();
- }
- public List query(Class clz, String scope) {
- return query(clz, scope, null);
- }
- public List query(Class clz, String scope, Collection paras) {
- return query(clz, scope, null, -1, -1);
- }
- public List query(Class clz, String scope, Collection paras, int begin,
- int max) {
- Query query = getEntityManager().createQuery(
- “from “ + clz + ” where “ + scope);
- int parameterIndex = 0;
- if (paras != null && paras.size() > 0) {
- for (Object obj : paras) {
- query.setParameter(parameterIndex++, obj);
- }
- }
- if (begin >= 0 && max > 0) {
- query.setFirstResult(begin);
- query.setMaxResults(max);
- }
- return query.getResultList();
- }
- public boolean save(Object obj) {
- getEntityManager().getTransaction().begin();
- getEntityManager().persist(obj);
- getEntityManager().getTransaction().commit();
- return true;
- }
- public Object uniqueResult(String sql) {
- return uniqueResult(sql, null);
- }
- public Object uniqueResult(String sql, Collection paras) {
- Query query = getEntityManager().createQuery(sql);
- int parameterIndex = 0;
- if (paras != null && paras.size() > 0) {
- for (Object obj : paras) {
- query.setParameter(parameterIndex++, obj);
- }
- }
- return query.getSingleResult();
- }
- public boolean update(Object obj) {
- getEntityManager().getTransaction().begin();
- getEntityManager().merge(obj);
- getEntityManager().getTransaction().commit();
- return true;
- }
- }
Not everyone can be formulated as a standard, but everyone has the right to the pursuit of standards and beyond the standard! !
