2

I am using Wildfly 8.1 and Postgres 9.2 with latest jdbc driver (non XA configured)

If I have a Session Bean like this:

@Stateless
public class MySessionBean {
  @Resource(lookup="jdbc/mydb")
  Datasource ds;

  @PersistenceContext // defaults to datasource lookup name "jdbc/mydb"
  EntityManager em;

  public void method1() {
    // will ds.getConnection() and 
    // the underlying connection used by em be the same?
  }
}

Will ds.getConnection() be the same underlying connection used by the EntityManager (em) inside the method call of method1() ?

If they share the same jta-transaction, how is it that I don't need XA datasources and just regular one ? (assuming the lookup name for the datasource is also de default datasource for the EntityManager)

I can't find the corelation in the ejb 3.1 specs.

Besides the question I understand that I need to close the datasource connection myself but the transaction will be managed by the container (correct me if I am wrong)

David Hofmann
  • 5,683
  • 12
  • 50
  • 78

1 Answers1

1

I don't think the connection is the same. As far as I understand you will get 2 different connections on the same pool which is managed by the application server.

If so you can't have a simple transaction shared across 2 connection. You must share the transaction manager between the 2 resources and so use XA.

Gab
  • 7,869
  • 4
  • 37
  • 68
  • 1
    It's quite easy to test update data using ds. connection and request it using a jpql query bypassing the EM cache, if update is visible then you share the connection / transaction – Gab Oct 22 '14 at 21:01
  • however I think the container should at least warn you if you are doing something wrong with the resources it manages. In this excact case I am using wildfly 8.1 server and it just works (with non XA datasource), like they are in the same jta transaction (not sure if they share the same connection yet, but I will find out) – David Hofmann Oct 24 '14 at 11:24
  • 1
    afak the transaction management for datasource is delegated to the underlying DBMS and you can't so share a transaction between multiple connection. Some DBMS provide features to do so (sqlServer for ex.), but I'am quite sure it's not supported at JTA level. If the JSR is not clear on this aspect it depends on the underlying app server implementation, seems that on wildfly the same connection is provided in this case – Gab Oct 24 '14 at 12:20
  • it is the same connection, I inserted one entity with EntityManager and then obtained a new connection using the new InitialContext().lookup() method, and the jdbc Connection can see the just inserted entity, but from my psql command line I still don't see it. So... (weird stuff that just works and I can't understand why this is right...) – David Hofmann Oct 25 '14 at 19:39
  • I edited the question showing I am using wildfly and postgres – David Hofmann Oct 25 '14 at 19:44
  • Thx for giving us your conclusion guy, it's usefull to know. And thx for the accept even if i don't deserve it :) – Gab Oct 27 '14 at 08:17