Glassfish - 资源注入不起作用

发布于 2024-10-02 03:55:32 字数 891 浏览 0 评论 0原文

使用 NetBeans IDE 和 Glassfish 服务器。由于某种原因,我无法注入数据源(已经尝试了一百万种变体)。

private DataSource iserver;
@Resource(name="jdbc/iserver", type=DataSource.class)
public void setIServer(DataSource dataSource) {
    this.iserver = dataSource;
}

(我已经尝试将 @Resource 注释直接添加到该字段)。连接池和jdbc资源在Glassfish上配置,暂时我添加了解决方法代码(在同一个类中):

ctx = new InitialContext();
iserver = (DataSource) ctx.lookup("jdbc/iserver");

解决方法代码工作完美。我在 Glassfish 日志中没有看到任何明显相关错误。我确实看到了这一点,但不确定它是否相关:

*name can not be null at javax.management.ObjectName.construct(ObjectName.java:405) * at javax.management.ObjectName.(ObjectName.java :1403)在org.glassfish.admingui.common.handlers.ProxyHandlers.getRuntimeProxyAttrs(ProxyHandlers.java:289)在org.glassfish.admingui.common.handlers.ProxyHandlers.getProxyAttrs(ProxyHandlers.java:273)在...

任何建议?

Using NetBeans IDE and Glassfish Server. For some reason I can't get a DataSource injected (have tried a million variations).

private DataSource iserver;
@Resource(name="jdbc/iserver", type=DataSource.class)
public void setIServer(DataSource dataSource) {
    this.iserver = dataSource;
}

(I already tried adding @Resource annotation directly to the field). The connection pool and jdbc resource are configured on Glassfish, and for the time being, I have added the workaround code (in the same class):

ctx = new InitialContext();
iserver = (DataSource) ctx.lookup("jdbc/iserver");

The workaround code works perfectly. I don't see any obvious relevant errors in Glassfish log. I do see this, but not sure it's related:

*name cannot be null at javax.management.ObjectName.construct(ObjectName.java:405) * at javax.management.ObjectName.(ObjectName.java:1403) at org.glassfish.admingui.common.handlers.ProxyHandlers.getRuntimeProxyAttrs(ProxyHandlers.java:289) at org.glassfish.admingui.common.handlers.ProxyHandlers.getProxyAttrs(ProxyHandlers.java:273) at ...

Any suggestions?

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

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

发布评论

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

评论(2

把时间冻结 2024-10-09 03:55:32

通过 "lookup" 选择选项 "name" -->

@Resource(lookup = "java:global/env/jdbc/__default")
DataSource dataSource;

Choose the option "name" by "lookup" -->

@Resource(lookup = "java:global/env/jdbc/__default")
DataSource dataSource;
瞳孔里扚悲伤 2024-10-09 03:55:32

确保您处于会话 bean 中,否则注入将不起作用。

这是我如何注入的示例

@Resource(name="jdbc/my_db") private DataSource dataSource;

Make sure you're in a session bean or injection won't work.

Here's an example of how I inject

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