18 January 2015

Glassfish missing DataSource: Table/View XXX does not exist

I have a little old webapp connecting to a DB using a glassfish ConnectionPool.
This used to run fine but on a more recent glassfish (4.0) I do net get a good datasource from Glassfish, resulting in a Table/View <name> does not exist error message (the message is a general symptom for a failed DB connection, which can have many other reasons).
The problem exists if I configure the DataSource in web.xml and also if I inject it using @Resource.
The solution is to explicitly map the application reference to the JNDI name in WEB-INF/glassfish-web.xml:

<resource-ref>
    <res-ref-name>jdbc/theDB</res-ref-name>
    <jndi-name>jdbc/theDB</jndi-name>
</resource-ref>  

If you don't, Glassfish returns a ConnectionPoolDataSource. Some people corrected this by refering in their web.xml to the ConnectionPoolDataSource, but the solution above will work both for web.xml and Resource injection.