Obtaining Connections from a DataSource


EJB design: Obtaining Connections from a DataSource

  1. Obtaining Connections from a DataSource (1 messages)

    We are having an issue in getting Connections from a DataSource. We are using WebSphere 3.5.3, although have seen this problem since 3.5.

    1) We get our DataSource from JNDI using (TDB2 is our DataSource name setup in WebSphere:
    Object o = getInitialContext().lookup("jdbc/TDB2"); We then narrow this to a DataSource.

    2) Using this DataSource, we get a Connection:
    java.sql.Connection c = dataSource.getConnection(userName, password);
    However, in order to use this, we need to get the username and password from somewhere, introducing a security risk.
    When setting up DataSources on EJBs (in WebSphere), a DataSource is associated with a Bean, and a USerName and Password is associated with it. I would like to then use this DataSource and get a Connection from it, but I have 2 problems (probably more, but I have not encountered them yet):
    a) How do I get the DataSource associated with the specific EJB Bean?
    b) How do I get the Connection from that DataSource? If I use the empty parm version: "dataSource.getConnection()", it does not use the correct userName/password (or I could be using the wrong DataSource) - I do not get a Connection.

    Thanks for any insight!
  2. Here is a possible solution. It is not perfect but it's one solution that can work. If you always use the same username and password to connect to a DB (you have a "shared" user) you can place the connection information in a .properties file and read them using a ResourceBundle.

    If you connect to your DB with the login name and password of every user then you have to get those values from you session object.

    The .properties file is not the perfect solution (specially talking about security) but if someone has a better suggestion please let me know because I'will run into a similar problem in a not-so-distant future.