Ordiel Ordiel - 4 years ago 300
Java Question

Inject hibernate PersistenceUnit

By using CDI as shown in the next code:

@PersistenceUnit
EntityManagerFactory emf;


I want to inject my hibernate
EntityManagerFactory


Currently if I execute the next line:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("HibernatePersistanceProv");


It works just as expected, but if I do it using the first method it tries to use the Derby connection I know this because I get the next error message:

org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection


By the stack trace I know that it is caused because of this.

Error connecting to server localhost on port 1527 with message Connection refused.


Which I know it is because it is trying to connect to the (Java DB) Derby db.

My persistance.xml looks like this:

<?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="HibernatePersistanceProv" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/aschema"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="false"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/aschema"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
</properties>
</persistence-unit>
</persistence>


I was reading that apparently I need to specify a
standalone.xml
to provide a different
<jta-data-source>
but it seams to me a bit more complicated than what it should be (I do not want to chase the wrong rabbit), I have been from the Java EE world for a while so I consider my self as brand new to this (for dummies explanations are widely appreciated).

(If it helps) I am running on a GlassFish 4.1 server. Please ask if any other information is required.

Answer Source

The solution end up being that I was not managing properly my connections pools in Glassfish, in order to achieve this behavior (at least this is the way I found, but I am pretty sure they should be more) you need to:

Glassfish side:

  1. In the "Common Tasks" Panel (left side of the administrator console (Glasfish4) expand JDBC.
  2. Select JDBC Connection Pools and click the New button on top of the main (central) panel, proceed to configure the data base connection for the pool.
  3. Now in the same JDBC section previously mentioned (left panel) select JDBC Resources (should be immediately above JDBC Connection Pools) there you can create a new resource so that you can use the CDI using the name of your choice OR as I did, just configure it in the jdbc/__default connection (as you may imagine that is the default connection provided by Glassfish CDI name space, to select your connection pool click on the link jdbc/__default on the table which appeared on the main (central) panel, that will take you into another form where you could use the drop down labeled as Pool Name: to select your newly configured connection pool, or the one of your choice; save it, top left of the main (central) panel.

Hibernate side:

  1. In the persistence.xml, you can either: a) Be sure that you are not providing any <jta-data-source> (IFF you configured in the jdbc/__default) b) Provide the JNDI Name (usually the name you provided for your JDBC Resources (in case you created one) or in the case of the default connection (jdbc/__default) you can see the JDNI name in the edit view (which is: java:comp/DefaultDataSource). Write that into <jta-data-source> in your persistence.xml and it should do the trick.

Sorry for the lack of graphical resources, I'll try to add them later. I hope it works for you "anonymous reader".

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download