Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EntityManager configuration in each DAO

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

I'm Using EclipseLink jpa version 2, Spring 3, jdk6, MySQL5 and tomcat7.

I have configured 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've got few confusion about what i have done:

  1. Is the EntityManager injected by Spring? (I understand that @PersistenceContext is a J2EE annotation, so wondering whether it is injected without Spring's contribution).

  2. As i have already mentioned, i have injected EntityManager in all the DAO classes. Is this a good practice? or should i make it Singleton by having a separate class like PersistenceManager, which has EntityManager attribute wired, and have getEntityManager() method?

  3. As you can see above, i have configured Spring transactions. But when i do CRUD operations continuously for 2-3 times, application gets stuck and fails with EclipseLink exception saying unable to get lock, timeout etc. Am i doing anything wrong here or missing any transaction configurations??

  4. With the above configurations, i can only use @Transactional annotation with default values which are PROPAGATION_REQUIRED,ISOLATION_DEFAULT. If i change these for any other values, such as @Transactional(PROPAGATION_REQUIRED,ISOLATION_SERIALIZABLE) etc, application throws exception as Custom isolation levels are not supported. Again, am i missing any configurations?

    Thanks.

like image 545
popcoder Avatar asked Feb 24 '23 01:02

popcoder


1 Answers

  1. Yes, spring recognizes the @PersistenceContext annotation and injects the entity manager
  2. Spring takes care of that - it injects the same EntityManager instance in all DAOs. In fact, it injects a proxy so that each request uses a different entity manager.
  3. Normally everything should run fine. You need <tx:annotation-driven /> in order to use @Transactional
  4. JPA only supports the default isolation level. You can work this around by customizing the spring jpa dialect, but there's nothing built-in. The way to go is extend XJpaDialect (in your case X=EclipseLink), override the beingTransaction, obtain the Connection (in an eclipse-link specific way), 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>
    
like image 65
Bozho Avatar answered Feb 25 '23 17:02

Bozho