1

I have a Java application which uses JDBC in order to connect to a Postgres database and JPA to perform operations on it. I wish to use the JTA transaction type, not the local one. For that, I need to specify a data source.

Despite reading this thread, I still have no idea what to actually put in the xml file, as I have no idea how to retrieve the name of my datasource, and/or where and how to define it.

Connection to the database already works without a problem when I use the RESOURCE-LOCAL transaction type. A lot of threads I skimmed through mentioned defining this in a file called context.xml. Does it have to be this file? As no such file has been auto generated for me when creating the JDBC database connection and I would need to create it manually.

In short, if it is possible to get the following file working by adding
<jta-data-source>something</jta-data-source>, please tell me what that something is, or how do I find out. Otherwise, please tell me how and where to define that something.

<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence 
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> 

<persistence-unit name="BankingPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>banking.Splatka</class>
    <class>banking.VlastnikKonta</class>
    <class>banking.FyzickaTransakce</class>
    <class>banking.Klient</class>
    <class>banking.PlatebniKarta</class>
    <class>banking.Transakce</class>
    <class>banking.Uver</class>
    <class>banking.Platba</class>
    <class>banking.Konto</class>
    <class>banking.BankovniPrevod</class>
    <class>DB_control.Transakceprevod</class>
    <class>banking.Transakceprevod</class>
    <class>banking.TransakcePrevod</class>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://xxxxxxxxxx"/>
      <property name="javax.persistence.jdbc.password" value="xxxx"/>
      <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
      <property name="javax.persistence.jdbc.user" value="xxxxxxxxxxx"/>
      <property name="javax.persistence.schema-generation.database.action" value="create"/>
    </properties>   </persistence-unit> </persistence>
Community
  • 1
  • 1
user129186
  • 1,156
  • 2
  • 14
  • 30

1 Answers1

2
<jta-data-source>something</jta-data-source>

something will be the jndi name of your datasource in J2EE enviornment.

When you are referring to any datasource you don't need to put below part in you persistence.xml.

<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://xxxxxxxxxx"/>
  <property name="javax.persistence.jdbc.password" value="xxxx"/>
  <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
  <property name="javax.persistence.jdbc.user" value="xxxxxxxxxxx"/>
Vijendra Kumar Kulhade
  • 2,217
  • 3
  • 15
  • 25
  • 1
    I am aware of the fact that it is the jndi name of a datasource. My problem is that I don't know what the name is, or even if I have one at all. How, and where would i define a data source which would hold information equivalent to the above ? – user129186 May 24 '16 at 00:31
  • If you are using tomcat to run your application then you need to create context.xml where you will create datasource and that datasource will come here in place of something. – Vijendra Kumar Kulhade May 24 '16 at 00:35
  • I am not using Tomcat to run my application as far as I am aware. This is a client application which connects to a remote db. Just to check, i tried doing exactly what you said, but got a "Cannot acquire data source [jdbc/Datasource]." exception. – user129186 May 24 '16 at 00:43
  • I gave just an example for application running on tomcat. You need to know the correct data source. – Vijendra Kumar Kulhade May 24 '16 at 00:46
  • Well, trying to figure out how to find the correct data source is the entire point of this post. Why is it relevant if the application is running tomcat or not? If i understand correctly, the resource is the database itself (or well, the jdbc connection to it). Shouldn't the JPA itself have a way of looking for the resource definitions? Well, I mean, it does that through JNDI, but should I not be capable of defining a JNDI resource through some xml or something regardless of whether I'm running Apache (or anything elseú or not? Some independent way. – user129186 May 24 '16 at 01:06
  • @VijendraKulhade is completely correct. You need to learn how to figure out how to create a datasource for whatever Java EE provider you are using (such as Glassfish, WebSphere,WildFly, Tomcat, etc). You are trying to use "Java EE JPA" here, so you need to be using a Java EE provider. It looks like you're trying to use Java EE JPA with only Java SE utilities. – Andy Guibert May 24 '16 at 03:10