0 Replies Latest reply on Nov 1, 2012 8:50 AM by tomas_toss

    Mapping global jndi to enc

    tomas_toss

      Hi!

       

      I apologize in advance if the answear to my question is obvious. I started to use jboss as7 (and Java EE)  last week, so I'm not that familiar with the terminology and concepts.

       

      I have made some sample ejb3 applications (like helloWorld) and have got them running. However, I'm still (very) confused regarding the configuration of the resources (datasource) and the corresponding injection in the beans.

       

      For instance, I have created a simple statless bean which only task is to extract some values from a mysql database:

       

      [code]

      package logic;
      
      
      import java.sql.Connection;
      import java.sql.ResultSet;
      import java.sql.SQLException;
      import java.sql.Statement;
      
      
      import javax.annotation.PostConstruct;
      import javax.annotation.PreDestroy;
      import javax.annotation.Resource;
      import javax.ejb.Remove;
      import javax.ejb.Stateful;
      import javax.sql.DataSource;
      
      
      /**
       * Session Bean implementation class SimpleBean
       */
      @Stateless
      public class SimpleBean implements SimpleBeanRemote {
      
      
                @Resource(name="mysqlDS", mappedName = "java:jboss/datasources/mysqlDS")
                private DataSource dataSource;
      
      
                private Connection connection;
      
      
                @PostConstruct
                public void openConnection() {
                          try {
                                    connection = dataSource.getConnection();
                          } catch (SQLException e) {
                                    e.printStackTrace();
                          }
                }
      
      
                @PreDestroy
                public void closeConnection() {
                          try {
                                    if(connection!=null){
                                              connection.close();
                                    }
                          } catch (SQLException e) {
                                    e.printStackTrace();
                          }
                }
      
      
                public String test() {
                          String msg = "";
                          try {
                                    Statement statement = connection.createStatement();
                                    String sql = "SELECT * from test";
                                    statement.execute(sql);
                                    ResultSet rs = statement.getResultSet();
                                    rs.first();
        
                                    msg = rs.getString(1);
                                    msg += " " + rs.getInt(2);
                          } catch (SQLException e) {
                                    e.printStackTrace();
                          }
      
                          return "SimpleBean: " + msg;
                }
      
      
      
                }
      }
      

      [/code]

       

      I have been able to run this code successfully, but I feel that the explicit lookup with jboss:java/datasources/mysqlDS is not a good (portable) solution. From what I have gathered, it is possible to map the global resource name to a logical application specific name (e.g. jdbc/mysql).

       

      I have tried using jboss-ejb3.xml and ejb-jar.xml to create this mapping, but can't figure out how to make it work. Am I on the right track, and if so could you give me a concrete example of such a mapping?

       

      (I find it very hard to find well structured information about these kind of issues. Are there any online resources for beginners that covers ejb3/jboss as7?)