views:

13

answers:

1

Hello,

Does anybody know whether I can use multiple data sources in a single stateless session bean in EJB3?

I want to develop a session bean to aggregate a lot data and return, but these data comes from 2 data sources, is this possible?

Can someone provide a sample on this?

Thanks very much !

A: 

Yes, as long as the DataSources are both capable of being enlisted in the same transaction (i.e., are XA capable). Simply create two separate resources, and bind them to separate DataSource objects.

@Resource
private DataSource ds1;
@Resource
private DataSource ds2;

public void businessMethod() {
    // ...use ds1 and ds2...
}

Unfortunately, data source creation and EJB bindings are vendor-specific, so it's not really possible to provide an example unless you mention which application server you're using.

bkail