0

If I mention both jta and non-jta datasource in persistence.xml, how will the provider identify what and when to use? Is there a way to enforce usage of non-jta datasource in certain scenario? I am using IBM supported OpenJPA.

Some providers allow to declare both a jta-datasource and a non-jta-datasource and use the later for optimized reading through non-JTA connections (i.e. that won't be associated to an ongoing JTA transaction). How this works and if OpenJPA supports this? Difference between a "jta-datasource" and a " resource-local " datasource?

Community
  • 1
  • 1
ad-inf
  • 1,520
  • 4
  • 30
  • 53

2 Answers2

2
  1. Probably what you want is to cofigure separate persistence units for the data sources & then injecting accordingly.

    <persistence-unit name="JTA_DS" transaction-type="JTA">
              <jta-data-source>java:JTA_DS</jta-data-source>
    </persistence-unit>
    <persistence-unit name="NON_JTA_DS" transaction-type="RESOURCE_LOCAL">
              <non-jta-data-source>java:NON_JTA_DS</non-jta-data-source>
    </persistence-unit>
    

    Now you can create EnityManager for respective persistence units.

    @PersistenceContext(unitName="JTA_DS")
    private EntityManager _JTAManager;

    @PersistenceContext(unitName="NON_JTA_DS")
    private EntityManager _NonJTAManager;

  2. Else you can build EntityManagerFactory manually as required using appropriate datasource. You can refer this link for more details.

Community
  • 1
  • 1
Nayan Wadekar
  • 11,444
  • 4
  • 50
  • 73
  • do you know if one safely can use the same datasource for both persistence units? I tried and seems to work fine.. – Jaqen H'ghar Nov 11 '14 at 19:41
  • @JaqenH'ghar It's vendor dependent, few might support it. Can refer here - http://stackoverflow.com/a/3218191/366964 ; which explains the difference & where it can be applicable. – Nayan Wadekar Nov 11 '14 at 22:17
0

It is difficult to understand why you mention both jta and non-jta datasource in persistence.xml. You don't need to declare both and you may need to declare one, jta or non-jta datasource.If you uses jta datasource, you will configure this in your application server and also declare in your persistence.xml.If you can't use jta datasource, you will configure JDBC connection in your persistence.xml.

Sai Ye Yan Naing Aye
  • 6,622
  • 12
  • 47
  • 65
  • It seems some provider optimizes use using non-jta datasource for executing queries which need not be included in transaction. Below URL has an example http://webspherepersistence.blogspot.in/. Looking for clarity on how this works and if this works. – ad-inf May 29 '12 at 10:15
  • http://stackoverflow.com/questions/3217586/difference-between-a-jta-datasource-and-a-resource-local-datasource - some providers even allow to declare both a jta-datasource and a non-jta-datasource and use the later for optimized reading through non-JTA connections (i.e. that won't be associated to an ongoing JTA transaction) – ad-inf May 29 '12 at 10:51