当通过glassfish注入时,Entitymanager为null

时间:2012-02-24 09:04:44

标签: ejb-3.0 glassfish-3 code-injection gwt-rpc entitymanager

* 当我在glassfish中运行项目时,entitymanager为null。如果我使用另一种东西而不是像servlet项目那样的工作 * < / p>

会话bean是:

@Stateless

public class logic implements logicLocal {

    @PersistenceContext(unitName="T2PU")
    private EntityManager em;

    @Override
    public void addToDB(Test t){

        em.persist(t);
    }

}

和GWT RPC是:

public class MainRPCImpl extends RemoteServiceServlet implements MainRPC {

    @EJB
    logicLocal logic;


    @Override
    public String addToDB(Test t) {
        String m="fail";
        try {
            logic.addToDB(t);
            m="done successfuly";
        } catch (Exception e) {
            return m;
        }
        return m;
    }
}

持久性单位是:

  <persistence-unit name="T2PU" transaction-type="JTA">
    <jta-data-source>acm</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties/>
  </persistence-unit>
</persistence>

和glassfish-resources

<resources>
    <jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="mysql_acm_rootPool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
        <property name="serverName" value="localhost"/>
        <property name="portNumber" value="3306"/>
        <property name="databaseName" value="acm"/>
        <property name="User" value="root"/>
        <property name="Password" value="1234"/>
        <property name="URL" value="jdbc:mysql://localhost:3306/acm"/>
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    </jdbc-connection-pool>
    <jdbc-resource enabled="true" jndi-name="acm" object-type="user" pool-name="mysql_acm_rootPool"/>
</resources>

特别感谢提前。

1 个答案:

答案 0 :(得分:0)

也许有不同的原因,但是你不需要在persistence.xml中为你的JNDI名称使用jdbc上下文前缀吗?像这样:

<jta-data-source>jdbc/acm</jta-data-source>

来自Glassfish administration guide

  

通过使用指定连接池来创建JDBC资源   资源将与哪个相关联。使用唯一的Java命名和   用于标识资源的目录接口(JNDI)名称。 ......因为   指定时,所有JNDI名称都在java:comp / env子上下文中   使用管理控制台中JDBC资源的JNDI名称   只有jdbc / name格式。