Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transaction is not getting tied to a Service bean while connecting to JPA using JpaTransactionManager [SPR-4254] #8932

Closed
spring-projects-issues opened this issue Dec 13, 2007 · 2 comments
Assignees
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx)

Comments

@spring-projects-issues
Copy link
Collaborator

Purnendu Das opened SPR-4254 and commented

The System is returning with The following exception stack trace

Exception in thread "main" org.springframework.dao.InvalidDataAccessApiUsageException: no transaction is in progress; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:289)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:97)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:212)
at org.springframework.orm.jpa.JpaAccessor.translateIfNecessary(JpaAccessor.java:152)
at org.springframework.orm.jpa.JpaTemplate.execute(JpaTemplate.java:190)
at org.springframework.orm.jpa.JpaTemplate.flush(JpaTemplate.java:281)
at javaapplication5.TripDaoimp.save(TripDaoimp.java:40)
at javaapplication5.TestService.save(TestService.java:35)
at javaapplication5.Test.setTrip(Test.java:32)
at javaapplication5.Main.main(Main.java:25)
Caused by: javax.persistence.TransactionRequiredException: no transaction is in progress
at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:294)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:346)
at $Proxy17.flush(Unknown Source)
at org.springframework.orm.jpa.JpaTemplate$8.doInJpa(JpaTemplate.java:283)
at org.springframework.orm.jpa.JpaTemplate.execute(JpaTemplate.java:185)
... 5 more

The classes are as below

/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.
    */

package javaapplication5;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
*

  • @author Purnendu
    */

public class Main {

/**
 * @param args the command line arguments
 */

public static void main(String[] args) {
    // TODO code application logic here
    ApplicationContext ctx =  new ClassPathXmlApplicationContext("applicationContext.xml");
    Test test = (Test) ctx.getBean("Test");
    test.setTrip(20);    
}

}

package javaapplication5;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
*

  • @author Purnendu
    */
    public class Test {

    static String text = "fhgvjhvhuj";
    String text1;
    private TestService testService;

    public Test() {
    text1 = "the initiated one";
    }

    String getTrip(Integer Id) {
    Trip trip = getTestService().findById(100);
    return trip.getDepcity();
    }

    void setTrip( Integer Id) {
    Trip t = new Trip(968);
    t.setPersonid(new Person(1));
    getTestService().save(t);
    }

    public TestService getTestService() {
    return testService;
    }

    public void setTestService(TestService testService) {
    this.testService = testService;
    }
    }

/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.
    */
    package javaapplication5;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
*

  • @author Purnendu
    */

public class TestService {

private TripDaoimp tripDaoimp;

public TripDaoimp getTripDaoimp() {
    return tripDaoimp;
}

public void setTripDaoimp(TripDaoimp tripDaoimp) {
    this.tripDaoimp = tripDaoimp;
}

Trip findById(int i) {
    return getTripDaoimp().findById(i);
}


void save( Trip t) {
    getTripDaoimp().save(t);
}

}

/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.
    */

package javaapplication5;

import java.util.List;
import org.springframework.orm.jpa.support.JpaDaoSupport;
import org.springframework.transaction.annotation.Transactional;
/**
*

  • @author Purnendu
    */
    public class TripDaoimp extends JpaDaoSupport implements TripDao {

    public Trip findById(Integer id) {
    return getJpaTemplate().find(Trip.class, id);

    }

    public List<Trip> findByName(String name) {
    throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<Trip> findByStreetName(String streetName) {
    throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<Trip> findByEntreeNameLike(String entreeName) {
    throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<Trip> findTripsWithVegetarianEntrees() {
    throw new UnsupportedOperationException("Not supported yet.");
    }

    public void save(Trip trip) {
    getJpaTemplate().persist(trip);
    getJpaTemplate().flush();
    }

    public Trip update(Trip Trip) {
    throw new UnsupportedOperationException("Not supported yet.");
    }

    public void delete(Trip Trip) {
    throw new UnsupportedOperationException("Not supported yet.");
    }

}

<?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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
file:///C:/Development/Library/spring-framework-2.5-with-dependencies/spring-framework-2.5/dist/resources/spring-beans-2.5.xsd
http://www.springframework.org/schema/jee
file:///C:/Development/Library/spring-framework-2.5-with-dependencies/spring-framework-2.5/dist/resources/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx
file:///C:/Development/Library/spring-framework-2.5-with-dependencies/spring-framework-2.5/dist/resources/spring-tx-2.5.xsd
">
<bean id='Test' class='javaapplication5.Test'>
<property name="testService" ref="TestService"/>
</bean>
<bean id='TestService' class='javaapplication5.TestService'>
<property name="tripDaoimp" ref="tripDaoimp"/>
</bean>
<bean id='tripDaoimp' class="javaapplication5.TripDaoimp">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="false" />
<property name="databasePlatform"
value="org.hibernate.dialect.MySQLDialect" />
</bean>
</property>
</bean>

<bean id="dataSource" 
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/hotel"/>
    <property name="username" value="root"/>
    <property name="password" value="admin123"/>
</bean>


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

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

</beans>
<?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="JavaApplication5PU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>javaapplication5.Person</class>
<class>javaapplication5.Trip</class>
<properties>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.password" value="admin123"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/hotel"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
</properties>
</persistence-unit>
</persistence>


Affects: 2.5 final

@spring-projects-issues
Copy link
Collaborator Author

Purnendu Das commented

Hi This problem was resolved by forcing the proxy creation on the class.

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Indeed - this seems to be a configuration problem. You need to make sure that your DAO actually executes within a transactional context, e.g. through the use of @Transactional on your service methods.

Juergen

@spring-projects-issues spring-projects-issues added type: bug A general bug in: data Issues in data modules (jdbc, orm, oxm, tx) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues removed the type: bug A general bug label Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx)
Projects
None yet
Development

No branches or pull requests

2 participants