cancel
Showing results for 
Search instead for 
Did you mean: 

Get BaseResourceException with JDBC ds.getConnection()

Former Member
0 Kudos

Hi,

I have created a stateless sessionbean with the intension to connect to the default SAP database by JDBC. Therefore I have created 3 project: a dictionairy project, a enterprise java bean project and a j2ee application project. Within the j2ee application project I have created a datasource alias:

?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE data-source-aliases SYSTEM "data-source-aliases.dtd">

<data-source-aliases>

<aliases>

<data-source-name>${com.sap.datasource.default}</data-source-name>

<alias>UNIQEMA_CONTACT_US</alias>

</aliases>

</data-source-aliases>

And in the ejb module project I have created also a reference to the datasource:

<resource-ref>

<res-ref-name>UNIQEMA_CONTACT_US</res-ref-name>

<res-type>javax.sql.DataSource</res-type>

<res-auth>Application</res-auth>

<res-sharing-scope>Shareable</res-sharing-scope>

</resource-ref>

I have also created the following create method in the ejb :

public void ejbCreate() throws CreateException {

try {

// Class.forName("com.sap.dbtech.jdbc.DriverSapDB");

Context ctx = new InitialContext();

DataSource ds = (DataSource) ctx.lookup("java:comp/env/UNIQEMA_CONTACT_US");

conn = ds.getConnection();

} catch (Exception e) {

throw new CreateException("e.getMessage");

}

}

But when the ejb excutes the ds.getConnection() I got the following error:

com.sap.engine.services.connector.exceptions.BaseResourceException: Cannot get connection. Reason: the thread of component "ejbContexts/sap.com/com.atosorigin.uniqema.contactus.appl/uniqema_contact_us_country" in application "sap.com/com.atosorigin.uniqema.contactus.appl" is currently associated with a restricted resource set and it is not allowed to get connection within it.

Please can someone say what I have done wrong a which feature I have forgotten to set.

regards,

Sander Valkenburg

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi sander,

Maybe this topic can help you!

Danny

Former Member
0 Kudos

Hi Danny,

I already discovered it myself. But many thanks for your solution, because it is exact that the problem I have.

Answers (3)

Answers (3)

kishorg
Advisor
Advisor
0 Kudos

Hi Sander Valkenburg ,

just try DataSource ds = (DataSource) ctx.lookup("jdbc/UNIQEMA_CONTACT_US"); or UNIQEMA_CONTACT_US alone as the look up string.

regards

Kishor Gopinathan

former_member184385
Active Participant
0 Kudos

Hi Sander,

another note on using DS connections:

1. connection as you may know are scarce resources, meant to be shared among apps. Storing a connection in a instance variable of a bean is not a good idea for another reason to: the database times out connections too!

2. proper usage would be something like this

Connection conn = null;

try {

// acquire conn.

conn = ds.getConnection();

// do something with it

..

} catch( ..) {

...

} finally {

// give conn back to pool

try { if () conn.close(); } catch(Exception e) {}

}

Regards

Gregor

former_member184385
Active Participant
0 Kudos

Hi Sander,

following suggestion:

1. write a lazy initializer/getter for your DS connection, named say, getLazyDs()

2. call getLazyDs() in one of your EJB's API methods

3. if the call in 2 succeeds, you are done, else there are issues either with the alias definition or reference.

Regards

Gregor