[jboss-user] [Beginner's Corner] - Mapping global jndi to enc

Tomas Toss do-not-reply at jboss.com
Thu Nov 1 08:50:55 EDT 2012


Tomas Toss [https://community.jboss.org/people/tomas_toss] created the discussion

"Mapping global jndi to enc"

To view the discussion, visit: https://community.jboss.org/message/773430#773430

--------------------------------------------------------------
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?)
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/773430#773430]

Start a new discussion in Beginner's Corner at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2075]

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-user/attachments/20121101/bfb0784d/attachment.html 


More information about the jboss-user mailing list