This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 91512 - Sample of JDBC resource injection?
Summary: Sample of JDBC resource injection?
Status: RESOLVED INVALID
Alias: None
Product: javaee
Classification: Unclassified
Component: Code (show other bugs)
Version: 5.x
Hardware: Macintosh All
: P3 blocker (vote)
Assignee: Andrei Badea
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-12-23 02:17 UTC by revans
Modified: 2007-01-02 14:30 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description revans 2006-12-23 02:17:49 UTC
Can you point me to a working sample project that uses a Derby... erm... javaDB
database and successfully creates and uses a connection using the new Java EE 5
@Resource annotation (i.e., resource injection).

I have a simple JSP-based project that uses JNDI lookup, and I want to simplify
my code using resource injection.

Specifically I here is the old JNDI code I have working 
/////////////
            // Retrieve the DataSource from JNDI
            Context ctx = new InitialContext();
            if ( ctx == null ) {
                throw new RuntimeException("JNDI Context could not be found.");
            }
            ds = (DataSource)ctx.lookup("java:comp/env/jdbc/leagueDB");
            if ( ds == null ) {
                throw new RuntimeException("DataSource could not be found.");
            }
            
            
            // Get a database connection
            connection = ds.getConnection();
////////

replace the above with something like this:
////////

    private @Resource(name="jdbc/leagueDB") DataSource ds;
    ...
    ...
            // Get a database connection
            connection = ds.getConnection();
////////

But the call to getConnection() fails.

Please point me to a working sample project, or some COMPLETE instructions.
Comment 1 revans 2006-12-23 03:03:14 UTC
Another way to pose the question might be:
Given a working database configuration,
how does one figure out the syntax for an @Resource statement?
-------
Given the following database properties:
   Database URL: jdbc:derby://localhost:1527/league
   Driver: org.apache.derby.jdbc.ClientDriver
and that this database connection is fully accessible from the 
NetBeans IDE under the runtime tab.
and it  works with my old JNDI lookup code.

How do I form a correct @Resource statement?
Comment 2 Andrei Badea 2007-01-02 14:30:10 UTC
1. Create a web project on Sun Java System Application Server, use Java EE 5 as
the Java EE version.

2. Create a servlet.

3. Right-click inside the servlet class, invoke Enterprise Resources - Use
Database. Choose the jdbc/sample data source. This generates a 

    @Resource(name = "jdbc/sample")
    private DataSource jdbcSample;

field.

4. You can now call jdbcSample.getConnection() somewhere in the processRequest()
method.

Note that dependency injection using annotations only works in managed classes
(e.g., servlets, JSF managed beans, EJB's).

Next time please ask questions on the nbusers@netbeans.org mailing list:

http://www.netbeans.org/community/index.html

Only file issues if the IDE is not working as expected, throws exceptions, etc.
Thanks.