1

Edit: I've update the post to reflect the questions in the comments below, but in summary, all of them are done but issue still exists

I'm trying to find a way to inject a Spring-managed EntityManager into my bean that handles the database update portion of a Spring Integration workflow.

For some reason, I keep getting a NullPointerException when trying to refer to the EM instance.

My setup is as follows:

@Component
public class BranchDeploymentUpdater {
     @PersistenceContext(unitName = "MyPU")
     private EntityManager em;

     public File handleUpdate(File input) {
         .....
         String query = "some query";
         TypedQuery<MyClass> typedQuery = em.CreateQuery(query, MyClass.class);
         .....
     }
}

My persistence.xml has been configured as follows:

<persistence-unit name="MyPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

    <properties>
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://192.168.2.169:3306/MYDB" />
        <property name="javax.persistence.jdbc.user" value="user" />
        <property name="javax.persistence.jdbc.password" value="P@ssw0rd" />

        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="true" />
        <!-- Connection Pooling -->
        <property name="hibernate.connection.provider_class"
              value="org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider" />
        <property name="hibernate.c3p0.max_size" value="100" />
        <property name="hibernate.c3p0.min_size" value="5" />
        <property name="hibernate.c3p0.acquire_increment" value="5" />
        <property name="hibernate.c3p0.idle_test_period" value="500" />
        <property name="hibernate.c3p0.max_statements" value="50" />
        <property name="hibernate.c3p0.timeout" value="10000" />
    </properties>

</persistence-unit>

My component scan is declared at the springapp-servlet.xml document as follows and the class using EM is confirmed to be in the package declared:

<context:component-scan base-package="com.myapp.webapp.controller, com.myapp.integration" />

The NPE would occur at the em.CreateQuerystatement.

In this same project, I also have a MVC webapp which I'm injecting the EM into the controller class using the exact same way and it works.

Can anybody give any pointers to where I may be getting it wrong?

Currently, I'm working around this by instantiating a new EM every time the bean gets invoked but this is causing an out-of-connection error with MySQL if I pump in too many transactions.

Please note that I'm not using Spring Integration's DB adapters as I already have the JPA code for handling the database layer and would like to keep that layer.

Thanks Wong

feicipet
  • 934
  • 2
  • 8
  • 21
  • How do you define the persistence unit in your persistence.xml? Maybe the persistence unit name does not match. – Tony Vu May 08 '15 at 02:13
  • Is the package where BranchDeploymentUpdater resides covered by the component scan? – shazin May 08 '15 at 02:14
  • I hope BranchDeploymentUpdater is a Spring bean, because at the snippet above isn't any @Component annotation or something like that. – mh-dev May 08 '15 at 03:52
  • Do you have registered the `PersistenceAnnotationBeanPostProcessor` as bean in the context where the `BranchDeploymentUpdater` is registered as Spring bean? Without that, the annotation `PersistenceContext` is not recognized by Spring. – dunni May 08 '15 at 05:19
  • @dunni No I did not register it. However, I was of the understanding that it is implied as long as I include it in my base component scan (Reference: http://stackoverflow.com/questions/12471019/is-persistenceannotationbeanpostprocessor-of-any-use-at-all) – feicipet May 08 '15 at 07:55
  • @TonyVu yes, it's been defined and I've checked that it matches, thanks! – feicipet May 08 '15 at 07:57
  • @shazin Yes, I've added this package into the component scan as updated in the original post, thanks! – feicipet May 08 '15 at 07:57
  • @mh-dev I forgot to mention the Component annotation bit in my code snippet but I did add it in. Alas, to no avail :( – feicipet May 08 '15 at 07:58
  • Please show the code where you call the `BranchDeploymentUpdater` and get the NPE. – chrylis -cautiouslyoptimistic- May 08 '15 at 09:06
  • Not sure if that help you, but I usually use `` for Spring Integration components. As well as for all others with `@Autowired`. – Artem Bilan May 08 '15 at 09:48
  • Probably you also check if you define your EntityManager factory with persistence unit correctly, It looks something like this: ` ..... ` – Tony Vu May 08 '15 at 10:52
  • Thank you all for your comments. I've finally been able to get it working by injecting an EntityManagerFactory instead, following the example set in http://docs.spring.io/spring/docs/2.5.x/reference/orm.html#orm-jpa-tx – feicipet May 14 '15 at 18:38

1 Answers1

0

Thanks for all the comments. I was able to finally get it working by injecting an EntityManagerFactory instead, following the example provided in http://docs.spring.io/spring/docs/2.5.x/reference/orm.html#orm-jpa-tx

I think one key thing that I had left out previously is the line:

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

This really takes a load of my mind as I was having a helluva time with race conditions in the DB instantiating my own EMs.

feicipet
  • 934
  • 2
  • 8
  • 21