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.CreateQuery
statement.
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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.