2

I'm having trouble trying to use the @Resource annotation with Java EE 6 and Glassfish 3.1 (embedded). I want to look up a JNDI datasource, so I'm trying to get it working with the default datasource in Glassfish. In my code I have:

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

It compiles fine. I'm using Maven + the advice here.

However, when I deploy my .war I always get the following error:

WARNING: Incorrect @Resource annotation class definition - missing lookup attribute
  symbol: FIELD
  location: javax.sql.DataSource ResourceLookup.dataSource

It's such a simple example that I'd be extremely surprised if it were a bug. I must be doing something wrong. Any ideas?

Ryan J
  • 2,502
  • 5
  • 31
  • 41

1 Answers1

3

As a test, can you try "jdbc/__default" as the lookup string? For my datasources I look them up using a jndi name I have specified, always with the pattern "jdbc/MyDataSourceName".

@Resource(lookup="jdbc/MyDataSourceName") works well. Never tried to look up the default ds, particularly not through the java:global namespace.

Jim
  • 3,476
  • 4
  • 23
  • 33
  • P.S. I hope you don't use the jdbc/__default in production, it is a derby database on a default install of Glassfish. – Jim Jun 21 '11 at 14:40
  • 1
    Looking at this further, when Glassfish shows this error, it's because it's getting a NoSuchMethodError exception, which means you're getting the wrong version of the Resource annotation compiled code. You've got classpath issues most likely. Do you have j2ee.jar bundled in your .war or something like that? Maybe try a fresh copy of Glassfish if your war just has your code and no j2ee or jee libraries in it. Also it's possible your JDK is too old. – Jim Jun 21 '11 at 14:52
  • 1
    See [Glassfish source](http://grepcode.com/file/maven.glassfish.org/content/repositories/glassfish-releases/org.glassfish.deployment/dol/3.0.1/com/sun/enterprise/deployment/annotation/handlers/ResourceHandler.java) At the bottom. This link is 3.0 code, and I can't quickly find the 3.1 code online, but I have it on my machine and the comment is: // Probably means lib endorsed dir is not set and an older version // of Resource is being picked up from JDK. // Don't treat this as a fatal error. – Jim Jun 21 '11 at 14:55
  • Jim, I'm only using jdbc/__default for convenience. You're right, java:global/ is not needed. Your 2nd and 3rd comments look correct. I'm using the maven-embedded-glassfish-plugin and, after a very quick glance at the source, I don't think it's setting java.endorsed.dirs. Everything works fine in a standalone Glassfish install. Thanks for pointing me in the right direction. – Ryan J Jun 21 '11 at 17:53
  • Worked, thanks! Was pondering over this since 3 days (GlassFish 4). – Nilesh Feb 18 '14 at 02:21