0

遵循此线程中给我的建议

我正在制作我的 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();
    }

}

编辑

好的,我简化了混乱

我所有的 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 等)

4

1 回答 1

1

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

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

默认情况下,如果未指定并发管理类型,则单例 bean 具有容器管理的并发划分。
...
如果未指定并发锁定属性,则假定为 Lock(WRITE)。bean 类上没有并发属性规范等价于 bean 类上的 Lock(WRITE) 规范。
...
如果容器调用与写入锁关联的方法,则在初始写入方法的处理完成之前,不允许进行其他并发调用。

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

JTA 事务通常涉及跨应用程序组件的调用。要完成 JTA 事务,这些组件通常需要访问单个持久性上下文。当通过 javax.persistence.PersistenceContext 注解将 EntityManager 注入应用程序组件时,就会发生这种情况。持久性上下文会随着当前 JTA 事务自动传播,映射到同一持久性单元的 EntityManager 引用提供对该事务内持久性上下文的访问。通过自动传播持久性上下文,应用程序组件不需要将 EntityManager 实例的引用传递给彼此,以便在单个事务中进行更改。Java EE 容器管理容器管理的实体管理器的生命周期。

于 2013-02-26T19:08:46.640 回答