0

I'm trying to instantiate an EntityManager using a JTA Datasource, but I always get NullPointerException. Here's my environment: Server : JBossAS 7.0 JPA : 2.0 DB : MySQL

persistence.xml

    <?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="WebStock" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>stockmanagementDS</jta-data-source>
        <class>prv.stockmanagement.business.Product</class>
    </persistence-unit>
</persistence>

On a dao class I'm trying this :

@PersistenceContext(type=PersistenceContextType.EXTENDED, unitName="WebStock")
private EntityManager em;

but when I use its getter, I get NullPointerException here :

public EntityManager getEntityManager() {
        return em;
    }

the debug shows it's null.

Here's the datasource definition in the standalone.xml :

<datasource jndi-name="stockmanagementDS" pool-name="stockmanagement" enabled="true" jta="true" use-java-context="true" use-ccm="true">
                    <connection-url>
                        jdbc:mysql://localhost:3306/kitchen_stock
                    </connection-url>
                    <driver>
                        mysql
                    </driver>
                    <security>
                        <user-name>
                            root
                        </user-name>
                        <password>
                            rootroot
                        </password>
                    </security>
                    <statement>
                        <prepared-statement-cache-size>
                            100
                        </prepared-statement-cache-size>
                        <share-prepared-statements/>
                    </statement>
                </datasource>

"mysql" is the name of the driver I defined after the datasource :

<drivers> <driver name="mysql" module="com.sql.mysql"> <driver-class> com.mysql.jdbc.Driver </driver-class> </driver> <driver name="h2" module="com.h2database.h2"> <xa-datasource-class> org.h2.jdbcx.JdbcDataSource </xa-datasource-class> </driver> </drivers>

Persistence is under META-INF, it's dynamic web project, and it's working well. The classing using the entity manager is not an EJB.

Any hint?

Iori Yagami
  • 387
  • 1
  • 3
  • 18

1 Answers1

0

Let me draw your attention to the following moments:

1. I don't see in your code driver for MySql to which you refer from datasource:

<driver>
   mysql
</driver>

You mast do follow:

Enter in the file path ${jboss_home}/modules and create the directories com/mysql/jdbc/main. Into the folder main copy the driver library jar (for example mysql-connector-java-5.1.38-bin.jar) and create a file module.xml:

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.0" name="com.mysql.jdbc">
  <resources>
     <resource-root path="mysql-connector-java-5.1.38-bin.jar"/>              
  </resources>
  <dependencies>
     <module name="javax.api"/>
  </dependencies>
</module>

And to the <drivers> tag in the standalone.xml add:

<driver name="mysql" module="com.mysql.jdbc">
  <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
</driver>

2. May be you should put your persistence.xml in your web module:

web
  src
    main
      java
        ..
      resources
        META-INF
          persistence.xml
      webapp
        ..

3. You can do inject EntiryManeger only into EJB or class with @Transactional annotation. Check to be sure your DAO class it is EJB bean.

HAYMbl4
  • 1,450
  • 2
  • 15
  • 29
  • 1 - Thank you for the answer. I edited my question so you can find the driver definition section and some more information. Should I use instead ? I'm using 5.1.18 version of mysql and I did the same as you showed (except of the path I used) 2 - and yes persistence is under META-INF folder. 3 - It's not an EJB, should it be? How can I use PersistenceUnit otherwise? – Iori Yagami Feb 22 '16 at 20:26
  • by 3 look http://stackoverflow.com/questions/4708035/persistencecontext-entitymanager-injection-nullpointerexception – HAYMbl4 Feb 24 '16 at 08:12