6 Replies Latest reply on Dec 6, 2010 7:50 AM by heiko.braun

    Errai + DataSource

    dan.dan

      Hi,

       

        I have a question about how to use database connection in Errai. Here is my source code as follows. When my client send message to this service, nothing happens. Is there anyone know how to deal with it or another way to use database connection?

      Thanks.

       

       

      import org.jboss.errai.bus.client.api.Message;

      import org.jboss.errai.bus.client.api.MessageCallback;

      import org.jboss.errai.bus.client.api.base.MessageBuilder;

      import org.jboss.errai.bus.client.framework.MessageBus;

      import org.jboss.errai.bus.server.annotations.Service;

       

      import java.sql.Connection;

      import java.sql.ResultSet;

      import java.sql.SQLException;

      import java.sql.Statement;

       

      import javax.annotation.Resource;

      import javax.enterprise.context.ApplicationScoped;

      import javax.inject.Inject;

      import javax.naming.InitialContext;

      import javax.sql.DataSource;

       

       

      @ApplicationScoped

      @Service

      public class HelloWorldService implements MessageCallback {

       

          @Inject

          MessageBus bus;

       

          public void callback(Message message) {

              String timemillis = Long.toString(System.currentTimeMillis());

              System.out.println("Received " + message.get(String.class, "payload"));

       

              getData();

       

              /* Conversation */

              MessageBuilder.createConversation(message).subjectProvided()

                      .signalling().with(

                      "response",    "Processed at " + timemillis + ", Danny said:"

                                      + message.get(String.class, "payload")).done()

                      .sendNowWith(bus);

          }

       

          public void getData(){

       

              Connection conn = null;

              try {

                  InitialContext ctx = new InitialContext();

                  DataSource datasource = (DataSource)ctx.lookup("jdbc/dsMyDB");

                  conn = datasource.getConnection();

                  System.out.println("Get Connection!");

              } catch (Exception e) {

                  // TODO Auto-generated catch block

                  e.printStackTrace();

              } finally {

                  try {

                      conn.close();

                  } catch (SQLException e) {

                      // TODO Auto-generated catch block

                      e.printStackTrace();

                  }

       

              }

          }

       

      }

        • 1. Re: Errai + DataSource
          heiko.braun

          Did you debug it already? What do you mean with "nothing happens"? No response or is the service code not executed?

          At the moment I don't see how this relates to the datasource question at all.

          • 2. Re: Errai + DataSource
            dan.dan

            Hi, Heiko

             

              Thanks for your reply. Let me describe my question more specifically:

             

              I use the following command to create an Errai project skeleton and deploy it into Jboss6 M3 successfully.

             

                mvn archetype:generate  -DarchetypeGroupId=org.jboss.errai -DarchetypeArtifactId=cdi-archetype -DarchetypeVersion=1.1-CR1 -DarchetypeRepository=https://repository.jboss.org/nexus/content/groups/public/

             

             

                And then, I want to use database in Errai server servicies so that client side can get information from database via Message Bus. Due to this reason, I added a new method getData() which can get DataSource and execute SQL statement. But when I put getData() into callback(Message message), client side won't get message from server service and there is no any error message displayed (that is what I mean "nothing happens" cause it used to get message from server). So, my question is how can I get data from database in Errai server servicies?

            Thanks a lot.

             

             

            public void getData(){

             

                    Connection conn = null;

                    try {

                        InitialContext ctx = new InitialContext();

                        DataSource datasource = (DataSource)ctx.lookup("jdbc/dsMyDB");

                        conn = datasource.getConnection();

                        Statement statement = conn.createStatement();

                        ResultSet rs1 = statement.executeQuery("Select * from myTable");

                        System.out.println("Get Connection!");

                    } catch (Exception e) {

                        // TODO Auto-generated catch block

                        e.printStackTrace();

                    } finally {

                        try {

                            conn.close();

                        } catch (SQLException e) {

                            // TODO Auto-generated catch block

                            e.printStackTrace();

                        }

             

                    }

                }

            • 3. Re: Errai + DataSource
              heiko.braun

              If ti works when not calling getData() I suggest you start a debugger and look where it fails.

              Seems like the stacktrace is lost somewhere. Did you look at the jboss logs?

              • 4. Re: Errai + DataSource
                dan.dan

                Hi, Heiko

                  Actually there is no any error message in jboss log during that time. I will continue to figure it out. Thanks a lot.

                • 5. Re: Errai + DataSource
                  dan.dan

                  Hi, Heiko

                   

                    I have found the way to deal with resource injection. It can inject resource such us datasource into Errai Server Service. There are offical information about CDI in here: Resource Injection

                   

                    Based on the document, I choose Field-Based Injection to implement as follows:

                   

                  @ApplicationScoped
                  @Service
                  public class HelloWorldService implements MessageCallback {

                   

                      @Inject
                      MessageBus bus;
                     
                      @Resource(name="dsMyOracle",mappedName="java:/dsMyOracle")
                      DataSource datasource;

                      ......

                      ......

                      ......

                   

                  Therefore, the field datasource is able to get connection without using InitialContext() to lookup resources.

                   

                              InitialContext ctx = new InitialContext();

                              DataSource datasource = (DataSource)ctx.lookup("jdbc/dsMyDB");

                              conn = datasource.getConnection();      // Get DB connection OK !

                  • 6. Re: Errai + DataSource
                    heiko.braun

                    cool. great to see people working with CDI