管理数据库实体管理器的好方法

时间:2013-02-26 18:03:44

标签: hibernate ejb persistence ejb-3.1

根据this thread

给我的建议

我正在制作我的DAO访问者@Singleton,@ Startup,@ LocalBean

我的问题是实现应该执行各种查询的entityManager的好方法。

知道有几个Dao,但它们都指向同一个entityManager(也是从单个entityManagerFactory创建的),最佳做法是什么?

到目前为止,这是我的实施,但我觉得它真是太糟糕了:))

道的

@Singleton
@LocalBean
@Startup
public class AircraftTypeDaoImpl extends DatabaseAccessor implements
        AircraftTypeDao {

    /** JPQL query to find a {@link AircraftType} given its OACI name */
    private static final String JPQL_FIND_BY_TYPE = "SELECT a FROM AircraftType a WHERE a.typeOACI=:typeOACI";

    /** JPQL query to find all {@link AircraftType} */
    private static final String JPQL_FIND_ALL = "SELECT a FROM AircraftType a";

    @Override
    public AircraftType find(String typeOACI) throws DAOException {
        AircraftType aircraftType = null;
        try {
            begin();
            Query findQuery = em.createQuery(JPQL_FIND_BY_TYPE).setParameter(
                    "typeOACI", typeOACI);
            aircraftType = (AircraftType) findQuery.getSingleResult();
        } catch (NoResultException e) {
            aircraftType = null;
            rollback();
        } catch (Exception e) {
            rollback();
            throw new DAOException(e);
        } finally {
            commit();
        }
        return aircraftType;
    }

    @Override
    public List<AircraftType> findAll() throws DAOException {
        List<AircraftType> types = null;
        try {
            begin();
            TypedQuery<AircraftType> findQuery = em.createQuery(JPQL_FIND_ALL,
                    AircraftType.class);
            types = findQuery.getResultList();
        } catch (NoResultException e) {
            types = new ArrayList<AircraftType>();
            rollback();
        } catch (Exception e) {
            rollback();
            throw new DAOException(e);
        } finally {
            commit();
        }
        return types;
    }

}

@Singleton
@LocalBean
@Startup
public class VariantDaoImpl extends DatabaseAccessor implements VariantDao {

    /** JPQL query to find a {@link Variant} given its variant name */
    private static final String JPQL_FIND_BY_NAME = "SELECT v FROM Variant v WHERE v.variantName=:variantName";

    /**
     * JPQL query to find all {@link Variant} given their associated
     * {@link AircraftType}
     */
    private static final String JPQL_FIND_BY_AC_TYPE = "SELECT v FROM Variant v WHERE v.type.typeOACI=:typeOACI";

    @Override
    public Variant find(String variantName) throws DAOException {
        Variant variant = null;
        try {
            begin();
            Query findQuery = em.createQuery(JPQL_FIND_BY_NAME).setParameter(
                    "variantName", variantName);
            variant = (Variant) findQuery.getSingleResult();
        } catch (NoResultException e) {
            variant = null;
            rollback();
        } catch (Exception e) {
            rollback();
            throw new DAOException(e);
        } finally {
            commit();
        }
        return variant;
    }

    @Override
    public List<Variant> find(AircraftType aircraftType) throws DAOException {
        List<Variant> variants = null;
        try {
            begin();
            TypedQuery<Variant> findQuery = em.createQuery(
                    JPQL_FIND_BY_AC_TYPE, Variant.class).setParameter(
                    "typeOACI", aircraftType.getTypeOACI());
            variants = findQuery.getResultList();
        } catch (NoResultException e) {
            variants = new ArrayList<Variant>();
            rollback();
        } catch (Exception e) {
            rollback();
            throw new DAOException(e);
        } finally {
            commit();
        }
        return variants;
    }

}

“母亲班”

public abstract class DatabaseAccessor {

    protected EntityManager em;

    private EntityTransaction tx;

    public DatabaseAccessor() {
        em = Persistence.createEntityManagerFactory("database")
                .createEntityManager();
    }

    public void begin() {
        tx = em.getTransaction();
        tx.begin();
    }

    public void commit() {
        tx.commit();
    }

    public void rollback() {
        tx.rollback();
    }

}

修改

好的,我简化了混乱

ALl我的DAO现在就像那样

@Singleton
@LocalBean
@Startup
public class AircraftTypeDaoImpl implements AircraftTypeDao {

    @PersistenceContext
    protected EntityManager em;

    /** JPQL query to find a {@link AircraftType} given its OACI name */
    private static final String JPQL_FIND_BY_TYPE = "SELECT a FROM AircraftType a WHERE a.typeOACI=:typeOACI";

    /** JPQL query to find all {@link AircraftType} */
    private static final String JPQL_FIND_ALL = "SELECT a FROM AircraftType a";

    @Override
    public AircraftType find(String typeOACI) throws DAOException {
        AircraftType aircraftType = null;
        try {
            Query findQuery = em.createQuery(JPQL_FIND_BY_TYPE).setParameter(
                    "typeOACI", typeOACI);
            aircraftType = (AircraftType) findQuery.getSingleResult();
        } catch (NoResultException e) {
            aircraftType = null;
        } catch (Exception e) {
            throw new DAOException(e);
        }
        return aircraftType;
    }

    @Override
    public List<AircraftType> findAll() throws DAOException {
        List<AircraftType> types = null;
        try {
            TypedQuery<AircraftType> findQuery = em.createQuery(JPQL_FIND_ALL,
                    AircraftType.class);
            types = findQuery.getResultList();
        } catch (NoResultException e) {
            types = new ArrayList<AircraftType>();
        } catch (Exception e) {
            throw new DAOException(e);
        }
        return types;
    }

}

我的persistence.xml就像那样

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="flightfaq">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.archive.autodetection" value="class, hbm"/>
            <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.connection.password" value="password"/>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/flightfaq"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name="hibernate.c3p0.min_size" value="5"/>
            <property name="hibernate.c3p0.max_size" value="20"/>
            <property name="hibernate.c3p0.timeout" value="300"/>
            <property name="hibernate.c3p0.max_statements" value="50"/>
            <property name="hibernate.c3p0.idle_test_period" value="3000"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="use_sql_comments" value="true"/>
            <property name="jadira.usertype.autoRegisterUserTypes" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

在TomEE上部署应用程序时,我得到了

26 févr. 2013 21:01:41 org.apache.openejb.config.ConfigurationFactory configureApplication
INFO: Configuring enterprise application: /Users/valentine/jbossworkspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/FlightFAQ
26 févr. 2013 21:01:43 org.apache.openejb.config.InitEjbDeployments deploy
INFO: Auto-deploying ejb CustomerDaoImpl: EjbDeployment(deployment-id=CustomerDaoImpl)
26 févr. 2013 21:01:43 org.apache.openejb.config.InitEjbDeployments deploy
INFO: Auto-deploying ejb AircraftTypeDaoImpl: EjbDeployment(deployment-id=AircraftTypeDaoImpl)
26 févr. 2013 21:01:43 org.apache.openejb.config.InitEjbDeployments deploy
INFO: Auto-deploying ejb VariantDaoImpl: EjbDeployment(deployment-id=VariantDaoImpl)
26 févr. 2013 21:01:43 org.apache.openejb.config.InitEjbDeployments deploy
INFO: Auto-deploying ejb FlightFaqUserDaoImpl: EjbDeployment(deployment-id=FlightFaqUserDaoImpl)
26 févr. 2013 21:01:43 org.apache.openejb.config.InitEjbDeployments deploy
INFO: Auto-deploying ejb AirportDaoImpl: EjbDeployment(deployment-id=AirportDaoImpl)
26 févr. 2013 21:01:43 org.apache.openejb.config.InitEjbDeployments deploy
INFO: Auto-deploying ejb LegDaoImpl: EjbDeployment(deployment-id=LegDaoImpl)
26 févr. 2013 21:01:43 org.apache.openejb.config.InitEjbDeployments deploy
INFO: Auto-deploying ejb MissionDaoImpl: EjbDeployment(deployment-id=MissionDaoImpl)
26 févr. 2013 21:01:43 org.apache.openejb.config.AutoConfig deploy
INFO: Configuring PersistenceUnit(name=flightfaq, provider=org.hibernate.ejb.HibernatePersistence)
26 févr. 2013 21:01:43 org.apache.openejb.config.AutoConfig setJtaDataSource
INFO: Adjusting PersistenceUnit flightfaq <jta-data-source> to Resource ID 'My DataSource' from 'null'
26 févr. 2013 21:01:43 org.apache.openejb.config.AutoConfig setNonJtaDataSource
INFO: Adjusting PersistenceUnit flightfaq <non-jta-data-source> to Resource ID 'My Unmanaged DataSource' from 'null'
26 févr. 2013 21:01:43 org.apache.openejb.config.AppInfoBuilder build
INFO: Enterprise application "/Users/valentine/jbossworkspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/FlightFAQ" loaded.
26 févr. 2013 21:01:43 org.apache.openejb.assembler.classic.Assembler createApplication
INFO: Assembling app: /Users/valentine/jbossworkspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/FlightFAQ
26 févr. 2013 21:01:44 org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
26 févr. 2013 21:01:44 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.9.Final}
26 févr. 2013 21:01:44 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
26 févr. 2013 21:01:44 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
26 févr. 2013 21:01:44 org.hibernate.ejb.Ejb3Configuration configure
INFO: HHH000204: Processing PersistenceUnitInfo [
    name: flightfaq
    ...]
26 févr. 2013 21:01:44 org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator instantiateExplicitConnectionProvider
INFO: HHH000130: Instantiating explicit connection provider: org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider
26 févr. 2013 21:02:05 com.sun.faces.mgbean.BeanManager addBean
ATTENTION: JSF1074 : Le bean géré nommé «startup» a déjà été enregistré.  Remplacement du type de classe du bean géré existant java.util.Date par java.util.Date.
26 févr. 2013 21:02:05 com.sun.faces.mgbean.BeanManager addBean
ATTENTION: JSF1074 : Le bean géré nommé «now» a déjà été enregistré.  Remplacement du type de classe du bean géré existant java.util.Date par java.util.Date.
26 févr. 2013 21:02:07 org.richfaces.application.InitializationListener logWarningWhenConnectionFactoryPresent
ATTENTION: JMS API was found on the classpath; if you want to enable RichFaces Push JMS integration, set context-param 'org.richfaces.push.jms.enabled' in web.xml
26 févr. 2013 21:02:07 org.apache.catalina.session.StandardManager doLoad
GRAVE: "ClassNotFoundException" lors du chargement de sessions persistantes: java.lang.ClassNotFoundException: com.flightfaq.dao.impl.AircraftTypeDaoImpl$LocalBeanProxy
java.lang.ClassNotFoundException: com.flightfaq.dao.impl.AircraftTypeDaoImpl$LocalBeanProxy
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
    at org.apache.tomee.catalina.LazyStopWebappClassLoader.loadClass(LazyStopWebappClassLoader.java:98)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:249)
    at org.apache.catalina.util.CustomObjectInputStream.resolveClass(CustomObjectInputStream.java:76)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1572)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1493)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1729)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1326)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1950)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1874)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1756)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1326)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:348)
    at java.util.HashMap.readObject(HashMap.java:1030)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:969)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1852)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1756)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1326)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1950)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1874)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1756)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1326)
    at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1664)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1320)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:348)
    at java.util.HashMap.readObject(HashMap.java:1030)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:969)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1852)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1756)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1326)
    at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1664)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1320)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:348)
    at java.util.HashMap.readObject(HashMap.java:1030)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:969)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1852)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1756)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1326)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:348)
    at java.util.HashMap.readObject(HashMap.java:1030)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:969)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1852)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1756)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1326)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:348)
    at org.apache.catalina.session.StandardSession.readObject(StandardSession.java:1595)
    at org.apache.catalina.session.StandardSession.readObjectData(StandardSession.java:1060)
    at org.apache.catalina.session.StandardManager.doLoad(StandardManager.java:284)
    at org.apache.catalina.session.StandardManager.load(StandardManager.java:204)
    at org.apache.catalina.session.StandardManager.startInternal(StandardManager.java:491)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5294)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:680)
26 févr. 2013 21:02:07 org.apache.catalina.session.StandardManager startInternal
GRAVE: Exception au chargement des sessions depuis le stockage persistant (persistent storage)
java.lang.ClassNotFoundException: com.flightfaq.dao.impl.AircraftTypeDaoImpl$LocalBeanProxy
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
    at org.apache.tomee.catalina.LazyStopWebappClassLoader.loadClass(LazyStopWebappClassLoader.java:98)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:249)
    at org.apache.catalina.util.CustomObjectInputStream.resolveClass(CustomObjectInputStream.java:76)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1572)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1493)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1729)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1326)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1950)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1874)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1756)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1326)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:348)
    at java.util.HashMap.readObject(HashMap.java:1030)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:969)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1852)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1756)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1326)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1950)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1874)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1756)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1326)
    at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1664)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1320)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:348)
    at java.util.HashMap.readObject(HashMap.java:1030)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:969)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1852)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1756)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1326)
    at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1664)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1320)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:348)
    at java.util.HashMap.readObject(HashMap.java:1030)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:969)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1852)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1756)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1326)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:348)
    at java.util.HashMap.readObject(HashMap.java:1030)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:969)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1852)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1756)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1326)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:348)
    at org.apache.catalina.session.StandardSession.readObject(StandardSession.java:1595)
    at org.apache.catalina.session.StandardSession.readObjectData(StandardSession.java:1060)
    at org.apache.catalina.session.StandardManager.doLoad(StandardManager.java:284)
    at org.apache.catalina.session.StandardManager.load(StandardManager.java:204)
    at org.apache.catalina.session.StandardManager.startInternal(StandardManager.java:491)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5294)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:680)

似乎各种DAO的部署正确,但是在我的Dao的LocalProxy版本上有一个类没找到异常? 你能解释一下吗? 网络服务器有问题吗?如何检查它是否与那种操作兼容(@PersistenceContext等)

1 个答案:

答案 0 :(得分:1)

它看起来有点像自动部分实现了JTA事务和容器管理持久化上下文开箱即用的东西。

此外,在此上下文中使用Singleton和默认并发(容器管理,WRITE)也会导致严重的性能损失,因为它限制了方法的并发执行。数据库查询可能需要相对较长的时间,并且可能没有理由在时间上仅执行其中一个。正如EJB 3.1规范中所述:

  

默认情况下,单例bean具有容器管理的并发性   如果未指定并发管理类型,则划分   ...
  如果未指定并发锁定属性,则假定为   要锁(写)。缺少并发属性   bean类的规范等同于规范   锁定(WRITE)bean类。
  ...
  如果容器调用与Write lock相关联的方法,则为no   其他并发调用将被允许继续进行,直到   初始Write方法的处理完成。

如果没有特别的理由不使用容器管理的EntityManager,以下Java EE 6 Tutorial的摘录解释了为什么不需要使用容器管理的EntityManager传递EntityManager的单个实例:

  

JTA事务通常涉及跨应用程序组件的调用。   要完成JTA事务,这些组件通常需要访问   单个持久化上下文。当EntityManager出现时会发生这种情况   通过以下方式注入应用程序组件   javax.persistence.PersistenceContext注释。坚持不懈   使用当前JTA事务自动传播上下文,   和映射到相同持久性的EntityManager引用   unit提供对其中持久性上下文的访问   交易。通过自动传播持久化上下文,   应用程序组件不需要传递对EntityManager的引用   彼此之间的实例,以便在单个内进行更改   交易。 Java EE容器管理生命周期   容器管理的实体经理。