Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k 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 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...