CuriousMind
CuriousMind

Reputation: 8903

Getting Datasource instance which is configured in Application Server?

I have configured a Datasource in JBOSS EAP 6.3.2 using Admin console.

I want to know what are the ways in which we can get the instance of this Datasource? Can we use @Resouce , @Inject annotations to get the Datasource?

Or should we use the old JNDI lookup?

Can anyone please help me understand this?

Upvotes: 0

Views: 469

Answers (2)

Rouliboy
Rouliboy

Reputation: 1377

I have never used Jboss EAP but as it's a Java EE 6 Server, you should be able to inject your datasource through @Resource annotation.

If it works the same as JBOSS AS 7, you should :

  • Define your datasource in standalone.xml
  • Inject the datasource specifying the lookup attribute of @Resource
@Resource(lookup = "java:jboss/MyDataSource")
private DataSource m_dataSource;

Upvotes: 1

Sudip
Sudip

Reputation: 92

I am using weblogic and I am using the lookup for getting connection. This may help you.

public Connection getConnection(){
    DataSource dataSource = null;
    Connection connection = null;
        try {
                InitialContext initContext;
                initContext = new InitialContext();
                dataSource = (DataSource) initContext.lookup("jdbc/MyDatasource");
            try {
                connection = dataSource.getConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }catch (Exception e) {
            e.printStackTrace();
        } 
    return connection;
}

Upvotes: 0

Related Questions