EntityManager configuration in each DAO

I understand that this is a very long question, but I wanted to ask everything because I was stuck with these things for more than 2 weeks, and I am in a situation to solve this within this week. Please help me in this matter.

I am using EclipseLink jpa version 2, Spring 3, jdk6, MySQL5 and tomcat7.

I set up the following in each of my DAO classes.

@PersistenceContext private EntityManager em; 

I have the following in my Spring xml:

 <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource"> <property name="url" value="jdbc:mysql://localhost:3306/xxxxx"/> <property name="username" value="xxxx"/> <property name="password" value="xxxx"/> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> </bean> <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory"> <property name="dataSource" ref="dataSource"/> <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/> <property name="jpaDialect" ref="jpaDialect"/> </bean> <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"/> <property name="jpaDialect" ref="jpaDialect"/> </bean> <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter" > <property name="showSql" value="true"/> <property name="generateDdl" value="true" /> </bean> <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect"/> 

From Persistence.xml:

 <persistence-unit name="xxxxx" transaction-type="RESOURCE_LOCAL"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> <-- class mappings --> </persistence-unit> 

I have little confusion about what I did:

  • Is EntityManager introduced by Spring? (I understand that @PersistenceContext is J2EE, so we wonder if it is being introduced without the input of Spring).

  • As I mentioned, I have introduced EntityManager in all DAO classes. Is this a good practice? or should I do this Singleton, having a separate class, for example PersistenceManager , which has the attribute EntityManager , and the getEntityManager() method?

  • As you can see above, I configured Spring transactions. But when I do CRUD operations continuously 2-3 times, the application gets stuck and fails with an EclipseLink exception saying it is unable to get a lock, timeout, etc. I am doing something wrong here or the absence of any transaction configurations

  • In the above configurations, I can only use the @Transactional annotation with the default values PROPAGATION_REQUIRED,ISOLATION_DEFAULT . If I change them for any other value, such as @Transactional(PROPAGATION_REQUIRED,ISOLATION_SERIALIZABLE) , etc., application exclusion is excluded, since custom isolation levels are not supported. Again, I do not see any configurations?

    Thanks.

+4
source share
1 answer
  • Yes, spring recognizes the @PersistenceContext annotation and introduces an entity manager
  • Spring will take care of this - it introduces the same EntityManager instance in all DAOs. In fact, it introduces a proxy server, so each request uses a different entity manager.
  • Usually everything should work fine. You need <tx:annotation-driven /> to use @Transactional
  • JPA only supports the default isolation level. You can do this by setting up spring jpa dialogs, but nothing is built in there. The path to the XJpaDialect extension (in your case X = EclipseLink), override beingTransaction , get Connection (in a specific eclipse method), set the desired isolation level (accessible through the transaction definition) and configure this as a property of your LocalContainerEntityManagerFactoryBean :

     <property name="jpaDialect"> <bean class="com.foo.util.persistence.EclipseLinkExtendedJpaDialect" /> </property> 
+8
source

All Articles