Quantcast
Channel: Spring Community Forums - Data
Viewing all articles
Browse latest Browse all 297

Potential EntityManager Bug

$
0
0
Hi,

We are having an issue after configuring our web service project to use Spring 3.2.3 and Hibernate 4.2.3 to connect to a MySQL database.

We are currently using the @PersistenceContext annotation to include the EntityManager in our endpoint.

When attempting to obtain the hibernate session from the EntityManager using getDelegate, we get a "org.hibernate.SessionException: Session is closed!", and when using unwrap(Session.class), we get "java.lang.IllegalStateException: No transactional EntityManager available".

However, when using entityManager directly, without obtaining the hibernate session, we are able to successfully query for objects using the find and query methods.


The full log with Spring Debug enabled when using entityManager.getDelegate:

http://pastebin.com/rXVs6AUf


The full log with Spring Debug enabled when using entityManager.unwrap:

http://pastebin.com/axyCJCQS

Here is our context configuration:

Code:

    <sws:annotation-driven/>
          <context:component-scan base-package="com.spaghetti"/>
   
        <context:spring-configured />

        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
                <property name="driverClass"><value>${database.driverClassName}</value></property>
                <property name="jdbcUrl"><value>${database.url}</value></property>
                <property name="user"><value>${database.username}</value></property>
                <property name="password"><value>${database.password}</value></property>
        <property name="initialPoolSize"><value>${hibernate.c3p0.initialPoolSize}</value></property>
        <property name="minPoolSize"><value>${hibernate.c3p0.minPoolSize}</value></property>
        <property name="maxPoolSize"><value>${hibernate.c3p0.maxPoolSize}</value></property>
        <property name="numHelperThreads"><value>${hibernate.c3p0.numHelperThreads}</value></property>
        <property name="acquireRetryAttempts"><value>${hibernate.c3p0.acquireRetryAttempts}</value></property>
        <property name="acquireIncrement"><value>${hibernate.c3p0.acquireIncrement}</value></property>
        <property name="idleConnectionTestPeriod" value="${hibernate.c3p0.idleConnectionTestPeriod}" />                 
        <property name="preferredTestQuery" value="${hibernate.c3p0.preferredTestQuery}" />
        <property name="testConnectionOnCheckin" value="${hibernate.c3p0.testConnectionOnCheckin}" />
        <property name="testConnectionOnCheckout" value="${hibernate.c3p0.testConnectionOnCheckout}" />
        <property name="maxConnectionAge" value="${hibernate.c3p0.maxConnectionAge}" />
        <property name="maxIdleTime" value="${hibernate.c3p0.maxIdleTime}" />
        </bean>
       
        <bean class="org.springframework.orm.jpa.JpaTransactionManager"
                id="transactionManager">
                <property name="entityManagerFactory" ref="entityManagerFactory" />
        </bean>

        <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager" />

        <util:map id="jpaPropertyMap">
        <entry key="generateDdl" value="true"/>
        <entry key="hibernate.hbm2ddl.auto" value="update"/>
        <entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
        <entry key="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />
        <entry key="hibernate.format_sql" value="false"/>
        <entry key="hibernate.show_sql" value="false" />       
    </util:map>
   
        <bean id="entityManagerFactory" name="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
                <property name="dataSource" ref="dataSource" />
                <property name="persistenceUnitName" value="puNeoDefault" />
                <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                    <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
                </bean>
                </property>
                <property name="packagesToScan" value="com.spaghetti"/>
                <property name="jpaPropertyMap" ref="jpaPropertyMap"/>
        </bean>
       
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
                <property name="dataSource" ref="dataSource" />
        </bean>
     
</beans>

And here is the endpoint class:

Code:

@Endpoint
public class EndpointTest {

        @PersistenceContext
        protected EntityManager entityManager;
       
        private static final String NAMESPACE_URI = "http://www.spaghetti.monster.com/testService";

        @PayloadRoot(namespace=NAMESPACE_URI, localPart="testRequest")
        @ResponsePayload
        @Transactional
        public TestResponse performTest(@RequestPayload TestRequest request) throws Exception{
               
                Session session = (Session) entityManager.getDelegate();
               
                Criteria criteria = session.createCriteria(TestEntity.class);
                criteria.list();
               
                return new TestResponse();
               
        }
       
}

If anyone has any ideas or has encountered this problem before, some suggestions would be very helpful.

We are not sure if this is a configuration problem or an issue with Spring / Hibernate / JPA.

If you would like to see / checkout the entire project, we can host this on GitHub.

Thanks.

Viewing all articles
Browse latest Browse all 297

Trending Articles