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:
-
Is the
EntityManagerinjected by Spring? (I understand that@PersistenceContextis a
J2EE annotation, so wondering whether it is injected without Spring’s contribution). -
As i have already mentioned, i have injected
EntityManagerin all the DAO classes. Is
this a good practice? or should i make it Singleton by having a separate class like
PersistenceManager, which hasEntityManagerattribute wired, and have
getEntityManager()method? -
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?? -
With the above configurations, i can only use
@Transactionalannotation with default
values which arePROPAGATION_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.
@PersistenceContextannotation and injects the entity managerEntityManagerinstance in all DAOs. In fact, it injects a proxy so that each request uses a different entity manager.<tx:annotation-driven />in order to use@TransactionalJPA 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 thebeingTransaction, obtain theConnection(in an eclipse-link specific way), set the desired isolation level (accessible through the transaction definition), and configure this as a property of yourLocalContainerEntityManagerFactoryBean: