Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

hibernate - Spring: TransactionRequiredException: No transaction is in progress at entitymanager.flush()

I know there are a lot of posts about this error. But nothing seems working for me. I am getting TransactionRequiredException at entitymanager.flush()

I have config class `

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
    entityManagerFactoryRef = “msEntityManagerFactory",
    transactionManagerRef = “msTransactionManager",
    basePackages = {
        x, y, z 
    })
public class msConfig {
    @Bean(name = “msDataSource")
    @ConfigurationProperties(prefix = "postgres.datasource.hikari")
    public DataSource msDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = “msEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(
                                                                       EntityManagerFactoryBuilder entityManagerFactoryBuilder,
                                                                       @Qualifier(“msDataSource") DataSource dataSource) {
        Properties properties = new Properties();
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");

        LocalContainerEntityManagerFactoryBean entityManager = entityManagerFactoryBuilder
                                                                                          .dataSource(dataSource)
                                                                                          .packages(“x”,”y”, “z”)
                                                                                          .persistenceUnit("postgres")
                                                                                          .build();

        entityManager.setJpaProperties(properties);

        return entityManager;
    }

    @Bean(name = “msTransactionManager")
    public PlatformTransactionManager transactionManager(@Qualifier(“msEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }
}

`

And In my service class, I am injecting EntityManager from above class like

public Dummy(@Qualifier(“msEntityManagerFactory") EntityManager entityManager) {
    this.entityManager = entityManager;
}

In same class, I have another method that is marked as @Transactional and has entityManager.flush()

What is missing?

Edit:

javax.persistence.TransactionRequiredException: no transaction is in progress
    at org.hibernate.internal.AbstractSharedSessionContract.checkTransactionNeededForUpdateOperation(AbstractSharedSessionContract.java:413)
    at org.hibernate.internal.SessionImpl.checkTransactionNeededForUpdateOperation(SessionImpl.java:3398)
    at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1355)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1350)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:366)
    at com.sun.proxy.$Proxy220.flush(Unknown Source)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:314)
    at com.sun.proxy.$Proxy220.flush(Unknown Source)
question from:https://stackoverflow.com/questions/65838054/spring-transactionrequiredexception-no-transaction-is-in-progress-at-entityman

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...