wood burning stoves 2.0*
The moose likes Spring and the fly likes Transaction is not being committed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Transaction is not being committed" Watch "Transaction is not being committed" New topic
Author

Transaction is not being committed

Naveen Jakad
Greenhorn

Joined: Jan 13, 2012
Posts: 6
Hi,
I am working on a project in which configuration is : Spring3.0 + JPA + hibernate

my persistence.xml is

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

<persistence-unit name="MyPersistentUnit" transaction-type="RESOURCE_LOCAL">

<provider>org.hibernate.ejb.HibernatePersistence</provider>

<class>com.hs18.recommendations.model.ViewPurchaseRecommendation</class>

<properties>
<property name="show_sql" value="true" />

<!-- Connection Details -->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />

<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/hsn18db_preprod" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="admin" />
<property name="hibernate.connection.autocommit" value="false" />

<property name="hibernate.default_batch_fetch_size" value="8" />
<property name="hibernate.jdbc.use_streams_for_binary" value="true" />
<property name="hibernate.jdbc.batch_size" value="50" />

<property name="hibernate.current_session_context_class" value="thread" />

<property name="hibernate.connection.pool_size" value="3" />
<property name="hibernate.c3p0.min_size" value="1" />
<property name="hibernate.c3p0.max_size" value="5" />
<property name="hibernate.c3p0.timeout" value="180" />
<property name="hibernate.c3p0.max_statements" value="50" />
<property name="hibernate.c3p0.acquire_increment" value="5" />

</properties>
</persistence-unit>
</persistence>


and beans.xml is:-

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:cxf="http://cxf.apache.org/core"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
default-lazy-init="false">

<!-- <import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> -->

<!-- <cxf:bus>
<cxf:features>
<cxf:logging/>
</cxf:features>
</cxf:bus> -->

<!-- <context:annotation-config /> -->

<!-- Plugin recommendations manager here. Easyrec is the current provider. -->
<bean id="recommendationsManager" class="com.hs18.recommendations.easyrec.manager.EasyRecRecommendationsManger"/>

<!-- Service definition -->
<bean id="recommendationBean" class="com.hs18.recommendations.impl.RecommendationsServiceImpl">
<property name="recommendationsManager" ><ref bean="recommendationsManager" /></property>
</bean>

<jaxws:endpoint id="recommendations" implementor="#recommendationBean" address="/Recommendations" />

<!-- JPA related definitions -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="MyPersistentUnit" />
</bean>

<bean name="viewPurchaseRecommendationDAO" class="com.hs18.recommendations.dao.ViewPurchaseRecommendationDAO">
<property name="jpaTemplate" ref="jpaTemplate"></property>
<property name="entityManagerFactory" ref="entityManagerFactory"></property>
</bean>

<bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
<property name="entityManagerFactory" ref="entityManagerFactory"></property>
</bean>

<bean id="myTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<!-- bean post-processor for JPA annotations -->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

<tx:annotation-driven transaction-manager="myTransactionManager" proxy-target-class="true"/>

</beans>


now when I try to persists any entity by following code:-

@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT,timeout=3)
public boolean saveViewPurchaseRecommendation(ViewPurchaseRecommendation recommendation){
if(recommendation!=null){
entityManagerFactory.createEntityManager().getTransaction().begin();
entityManagerFactory.createEntityManager().persist(recommendation);
entityManagerFactory.createEntityManager().flush();
return true;
}
return false;
}

entity doesn't get stored in DB, I don't know why .....please help me .........and let me know where things are going wrong......
Naveen Jakad
Greenhorn

Joined: Jan 13, 2012
Posts: 6
In fact I am getting exception that :


Jan 16, 2012 4:31:08 PM org.hibernate.annotations.common.Version <clinit>
INFO: Hibernate Commons Annotations 3.2.0.Final
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.6.6.Final
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : javassist
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
Jan 16, 2012 4:31:08 PM org.hibernate.ejb.Version <clinit>
INFO: Hibernate EntityManager 3.4.0.GA
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.AnnotationBinder bindClass
INFO: Binding entity from annotated class: com.hs18.recommendations.model.ViewPurchaseRecommendation
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.annotations.EntityBinder bindTable
INFO: Bind entity com.hs18.recommendations.model.ViewPurchaseRecommendation on table VIEW_PURCHASE_RECOMMENDATIONS
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.Configuration applyHibernateValidatorLegacyConstraintsOnDDL
INFO: Hibernate Validator not found: ignoring
Jan 16, 2012 4:31:08 PM org.hibernate.ejb.Ejb3Configuration configure
WARNING: hibernate.connection.autocommit = false break the EJB3 specification
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.search.HibernateSearchEventListenerRegister enableHibernateSearch
INFO: Unable to find org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not enabled.
Jan 16, 2012 4:31:08 PM org.hibernate.connection.ConnectionProviderFactory c3p0ProviderPresent
WARNING: c3p0 properties is specificed, but could not find org.hibernate.connection.C3P0ConnectionProvider from the classpath, these properties are going to be ignored.
Jan 16, 2012 4:31:08 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Using Hibernate built-in connection pool (not for production use!)
Jan 16, 2012 4:31:08 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Hibernate connection pool size: 3
Jan 16, 2012 4:31:08 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: autocommit mode: false
Jan 16, 2012 4:31:08 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/hsn18db_preprod
Jan 16, 2012 4:31:08 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: connection properties: {user=root, password=****, autocommit=false, release_mode=auto}
Jan 16, 2012 4:31:08 PM org.hibernate.dialect.Dialect <init>
INFO: Using dialect: org.hibernate.dialect.MySQLDialect
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Database ->
name : MySQL
version : 5.0.45-community-nt
major : 5
minor : 0
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Driver ->
name : MySQL-AB JDBC Driver
version : mysql-connector-java-5.1.17 ( Revision: ${bzr.revision-id} )
major : 5
minor : 1
Jan 16, 2012 4:31:08 PM org.hibernate.transaction.TransactionFactoryFactory buildTransactionFactory
INFO: Transaction strategy: org.hibernate.transaction.JDBCTransactionFactory
Jan 16, 2012 4:31:08 PM org.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
INFO: No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic flush during beforeCompletion(): disabled
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic session close at end of transaction: disabled
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch size: 50
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch updates for versioned data: disabled
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Scrollable result sets: enabled
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC3 getGeneratedKeys(): enabled
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Connection release mode: auto
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Maximum outer join fetch depth: 2
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default batch fetch size: 8
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Generate SQL with comments: disabled
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL updates by primary key: disabled
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL inserts for batching: disabled
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.SettingsFactory createQueryTranslatorFactory
INFO: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
Jan 16, 2012 4:31:08 PM org.hibernate.hql.ast.ASTQueryTranslatorFactory <init>
INFO: Using ASTQueryTranslatorFactory
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query language substitutions: {}
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JPA-QL strict compliance: enabled
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Second-level cache: enabled
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query cache: disabled
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.SettingsFactory createRegionFactory
INFO: Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Optimize cache for minimal puts: disabled
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Structured second-level cache entries: disabled
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Statistics: disabled
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Deleted entity synthetic identifier rollback: disabled
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default entity-mode: pojo
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Named query checking : enabled
Jan 16, 2012 4:31:08 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Check Nullability in Core (should be disabled when Bean Validation is on): enabled
Jan 16, 2012 4:31:08 PM org.hibernate.impl.SessionFactoryImpl <init>
INFO: building session factory
Jan 16, 2012 4:31:08 PM org.hibernate.type.BasicTypeRegistry register
INFO: Type registration [materialized_clob] overrides previous : org.hibernate.type.MaterializedClobType@18f2225f
Jan 16, 2012 4:31:08 PM org.hibernate.type.BasicTypeRegistry register
INFO: Type registration [blob] overrides previous : org.hibernate.type.BlobType@40ae97c4
Jan 16, 2012 4:31:08 PM org.hibernate.type.BasicTypeRegistry register
INFO: Type registration [java.sql.Blob] overrides previous : org.hibernate.type.BlobType@40ae97c4
Jan 16, 2012 4:31:08 PM org.hibernate.type.BasicTypeRegistry register
INFO: Type registration [materialized_blob] overrides previous : org.hibernate.type.MaterializedBlobType@66de609
Jan 16, 2012 4:31:08 PM org.hibernate.type.BasicTypeRegistry register
INFO: Type registration [characters_clob] overrides previous : org.hibernate.type.PrimitiveCharacterArrayClobType@19195c2b
Jan 16, 2012 4:31:08 PM org.hibernate.type.BasicTypeRegistry register
INFO: Type registration [clob] overrides previous : org.hibernate.type.ClobType@4ff217ec
Jan 16, 2012 4:31:08 PM org.hibernate.type.BasicTypeRegistry register
INFO: Type registration [java.sql.Clob] overrides previous : org.hibernate.type.ClobType@4ff217ec
Jan 16, 2012 4:31:08 PM org.hibernate.type.BasicTypeRegistry register
INFO: Type registration [wrapper_characters_clob] overrides previous : org.hibernate.type.CharacterArrayClobType@1f00aff5
Jan 16, 2012 4:31:08 PM org.hibernate.type.BasicTypeRegistry register
INFO: Type registration [wrapper_materialized_blob] overrides previous : org.hibernate.type.WrappedMaterializedBlobType@66200db9
Jan 16, 2012 4:31:09 PM org.hibernate.impl.SessionFactoryObjectFactory addInstance
INFO: Not binding factory to JNDI, no JNDI name configured
Exception in thread "main" javax.persistence.TransactionRequiredException: no transaction is in progress
at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:301)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:358)
at $Proxy9.flush(Unknown Source)
at com.hs18.recommendations.dao.ViewPurchaseRecommendationDAO.saveViewPurchaseRecommendation(ViewPurchaseRecommendationDAO.java:50)
at com.hs18.recommendations.dao.ViewPurchaseRecommendationDAO.main(ViewPurchaseRecommendationDAO.java:70)


Sending you entire stacktrace please help me out soon.......
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Naveen, please use the CODE buttons to post code and stacktraces so it is readable to us.

Thanks

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Atul Kotwale
Greenhorn

Joined: Dec 22, 2011
Posts: 17
The problem is in following line.



The entity manager created by above code does not participate in spring managed transaction. Get the transaction using following method.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Transaction is not being committed
 
Similar Threads
WebServiceContext or handle to session in CXF JAX-RS implementation
Accessing HttpServletRequest from Restful webservice
org.springframework.beans.factory.BeanCreationException
MessageContext is null
spring wont inject the entitymanager