通过 glassfish 注入时 Entitymanager 为 null

发布于 2025-01-08 07:56:41 字数 2702 浏览 0 评论 0原文

*当我在 glassfish 中部署项目时,entitymanager 为 null。如果我使用其他东西而不是像 servlet 之类的 RPC,项目工作可能 *

会话 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>

特别感谢。

*when i deploy project in glassfish entitymanager is null.if i use another thing instead of RPC like servlet project work probably *

session bean is:

@Stateless

public class logic implements logicLocal {

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

    @Override
    public void addToDB(Test t){

        em.persist(t);
    }

}

and GWT RPC is :

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 is :

  <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>

and 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>

specially thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

◇流星雨 2025-01-15 07:56:41

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

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

来自 Glassfish 管理指南 :

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

Maybe there are different reasons, but don't you need to use the jdbc context prefix for your JNDI name in persistence.xml? Something like this:

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

From the Glassfish administration guide:

A JDBC resource is created by specifying the connection pool with
which the resource will be associated . Use a unique Java Naming and
Directory Interface (JNDI) name to identify the resource. ... Because
all JNDI names are in the java:comp/env subcontext, when specifying
the JNDI name of a JDBC resource in the Administration Console, use
only the jdbc/name format.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文