Cannot connect to the database

I am trying to get a very simple (first) application working, and am stuck trying to connect to the database.

I am using Wildfly 8.2, with an XA data source, and running IntelliJ Idea IDE.

I have the data source deploying per the console log:

[code]
11:09:09,404 INFO [org.jboss.as.connector.deployers.jdbc]
(MSC service thread 1-7) JBAS010403: Deploying JDBC-compliant driver class oracle.jdbc.OracleDriver (version 11.2)
11:09:09,414 INFO [org.jboss.as.connector.deployers.jdbc]
(MSC service thread 1-1) JBAS010417: Started Driver service with driver-name = ojdbc6.jar
11:09:09,427 INFO [org.jboss.as.connector.subsystems.datasources]
(MSC service thread 1-8) JBAS010400: Bound data source [java:jboss/datasources/xads_kankan]

11:09:09,479 INFO [org.jboss.as.server]
(Controller Boot Thread) JBAS018559: Deployed “ojdbc6.jar” (runtime-name : “ojdbc6.jar”)
[/code]Note that the connection tests successfully via the console.

Here is the complete application code, which uses the Data Source:

[code]
public class StartApp extends UI
{
@Override
public void init(VaadinRequest request)
{
VerticalLayout layout = new VerticalLayout();
setContent(layout);
layout.addComponent(new Label(“A table”));

  J2EEConnectionPool pool = new J2EEConnectionPool("java:jboss/datasources/xads_kankan");

  TableQuery tq = new TableQuery("node_types", pool);
  SQLContainer container = null;
  try
  {
     container = new SQLContainer(tq);
  }
  catch (SQLException e)
  {
     e.printStackTrace();
  }

  Table t = new Table();
  t.setContainerDataSource(container);

  layout.addComponent(t);

}
}
[/code]And this is the error I get when trying to run the application (excerpt of much longer stack trace):

[code]
11:09:12,970 SEVERE [com.vaadin.server.DefaultErrorHandler]
(default task-7) : java.lang.RuntimeException: java.sql.SQLException: NamingException - Cannot connect to the database. Cause: null
at com.vaadin.data.util.sqlcontainer.query.TableQuery.fetchMetaData(TableQuery.java:659) [vaadin-server-7.3.7.jar:7.3.7]

at com.vaadin.data.util.sqlcontainer.query.TableQuery.<init>(TableQuery.java:189) [vaadin-server-7.3.7.jar:7.3.7]

at com.vaadin.data.util.sqlcontainer.query.TableQuery.<init>(TableQuery.java:127) [vaadin-server-7.3.7.jar:7.3.7]

at com.vaadin.data.util.sqlcontainer.query.TableQuery.<init>(TableQuery.java:105) [vaadin-server-7.3.7.jar:7.3.7]

at com.vaadin.data.util.sqlcontainer.query.TableQuery.<init>(TableQuery.java:147) [vaadin-server-7.3.7.jar:7.3.7]

at com.kankan.ui.StartApp.init(StartApp.java:26) [classes:]


Caused by: java.sql.SQLException: NamingException - Cannot connect to the database. Cause: null
at com.vaadin.data.util.sqlcontainer.connection.J2EEConnectionPool.lookupDataSource(J2EEConnectionPool.java:61) [vaadin-server-7.3.7.jar:7.3.7]

at com.vaadin.data.util.sqlcontainer.connection.J2EEConnectionPool.getDataSource(J2EEConnectionPool.java:51) [vaadin-server-7.3.7.jar:7.3.7]

[/code]I have also tried various other versions of the JNDI name but always get the same result. I imagine there is something simple here - any ideas what needs to be fixed?

Note there is another error that shows up during deployment, though I think it is unrelated:

11:09:12,360 SEVERE [org.atmosphere.cpr.AtmosphereFramework]
 (default task-1) Failed to initialize Atmosphere Framework: java.lang.IllegalArgumentException: Unable to createclass org.atmosphere.container.JettyServlet30AsyncSupportWithWebSocket
    at org.atmosphere.cpr.DefaultAsyncSupportResolver.newCometSupport(DefaultAsyncSupportResolver.java:229) [atmosphere-runtime-2.1.2.vaadin6.jar:2.1.2.vaadin6]

    at org.atmosphere.cpr.DefaultAsyncSupportResolver.resolveWebSocket(DefaultAsyncSupportResolver.java:288) [atmosphere-runtime-2.1.2.vaadin6.jar:2.1.2.vaadin6]

Thanks,
ken

It seems like the JNDI lookup code used by Vaadin cannot see any JBoss (WildFly) datasources, for some reason.

Is there some configuration I need to add somewhere to assist Vaadin in making JBoss datasources accessible?

After tweaking the com.vaadin.data.util.sqlcontainer.connection.J2EEConnectionPool lookupDataSource method so that the cause exception is not lost, I was able to get to the root exception, which is:

09:09:21,638 ERROR [stderr]
 (default task-7) Caused by: javax.naming.NameNotFoundException; remaining name 'kankan'
09:09:21,639 ERROR [stderr]
 (default task-7)     at org.eclipse.jetty.jndi.local.localContextRoot.lookup(localContextRoot.java:432)
09:09:21,639 ERROR [stderr]
 (default task-7)     at org.eclipse.jetty.jndi.local.localContextRoot.lookup(localContextRoot.java:536)
09:09:21,639 ERROR [stderr]
 (default task-7)     at javax.naming.InitialContext.lookup(InitialContext.java:417)
09:09:21,639 ERROR [stderr]
 (default task-7)     at javax.naming.InitialContext.lookup(InitialContext.java:417)
09:09:21,639 ERROR [stderr]
 (default task-7)     at com.vaadin.data.util.sqlcontainer.connection.J2EEConnectionPool.lookupDataSource(J2EEConnectionPool.java:59)

(Note that I have changed to a different data source to simplify - the new DS is not XA).

I am guessing that the org.eclipse.jetty JNDI lookup code simply does not or cannot access JBoss data sources.

I can’t seem to find any complete examples of the use of the J2EEConnectionPool code anywhere.

I determined that the issue is that the jetty JNDI lookup code is being used in preference to the JBoss JNDI lookup code.

The solution was to remove the jetty JNDI jar from the deployed application.

There are a number of jetty jar files in the full distribution. I don’t know what their purpose is, but at least for this simple application removing the JNDI jar eliminated the problem without causing any other issues. Perhaps I can safely remove all the jetty jars from the deployed application, if it is deployed to an application server like JBoss.

Thanks Ken Clark your solution also saved me !!