Spring Boot 升级到2.6.2后,没有正在进行的事务

ubof19bj  于 2023-01-20  发布在  Spring
关注(0)|答案(1)|浏览(159)

将 Spring Boot 从2.1.2.RELEASE升级到2.6.2。还根据Spring启动文档升级了休眠和hazelcast。这在2.1.2.RELEASE中工作正常,但无法启动应用程序。获取以下错误

/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.6.2)

Spring Boot version 2.6.2

2022-03-24 16:23:24.356 INFO  [main] org.springframework.context.annotation.ConfigurationClassParser(:469) - Properties location [file:${CACHE_CONFIG}] not resolvable: Could not resolve placeholder 'CACHE_CONFIG' in value "file:${CACHE_CONFIG}"
2022-03-24 16:23:26.222 INFO  [main] org.springframework.data.repository.config.RepositoryConfigurationDelegate(:132) - Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2022-03-24 16:23:26.632 INFO  [main] org.springframework.data.repository.config.RepositoryConfigurationDelegate(:201) - Finished Spring Data repository scanning in 378 ms. Found 9 JPA repository interfaces.
2022-03-24 16:23:33.400 INFO  [main] org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean(:437) - Initialized JPA EntityManagerFactory for persistence unit 'default'
2022-03-24 16:23:33.585 INFO  [main] org.keycloak.adapters.springboot.MultiTenantKeycloakConfiguration(:31) - setting keycloakproperties to multitenant keycloak config resolver
2022-03-24 16:23:34.253 WARN  [main] io.undertow.websockets.jsr(:68) - UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used
2022-03-24 16:23:34.327 INFO  [main] org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext(:290) - Root WebApplicationContext: initialization completed in 10810 ms
2022-03-24 16:23:35.151 INFO  [main] org.springframework.boot.web.servlet.RegistrationBean(:50) - Filter keycloakAuthenticationProcessingFilter was not registered (disabled)
2022-03-24 16:23:35.153 INFO  [main] org.springframework.boot.web.servlet.RegistrationBean(:50) - Filter keycloakPreAuthActionsFilter was not registered (disabled)
2022-03-24 16:23:35.604 INFO  [main] com.XXXX.smp.distributed.cache.hazelcast.HazelCastSpringConfig(:157) - Hazelcast started with database discovery mode.
2022-03-24 16:23:35.746 INFO  [main] com.XXXX.smp.distributed.cache.hazelcast.HazelCastSpringConfig(:104) - Default Configuration for hazelcast loaded
2022-03-24 16:23:35.761 INFO  [main] com.XXXX.smp.distributed.cache.hazelcast.HazelCastSpringConfig(:121) - Configuring cache :CacheProperties [name=PortalAssetLock, idleTimeoutMilliSeconds=600000, maxTimeoutMilliSeconds=-1]
2022-03-24 16:23:35.801 INFO  [main] com.XXXX.smp.distributed.cache.hazelcast.HazelCastSpringConfig(:121) - Configuring cache :CacheProperties [name=notification_cache, idleTimeoutMilliSeconds=-1, maxTimeoutMilliSeconds=60000]
2022-03-24 16:23:35.836 WARN  [main] com.hazelcast.instance.impl.HazelcastInstanceFactory(:56) - Hazelcast is starting in a Java modular environment (Java 9 and newer) but without proper access to required Java packages. Use additional Java arguments to provide Hazelcast access to Java internal API. The internal API access is used to get the best performance results. Arguments to be used:
 --add-modules java.se --add-exports java.base/jdk.internal.ref=ALL-UNNAMED --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.nio=ALL-UNNAMED --add-opens java.base/sun.nio.ch=ALL-UNNAMED --add-opens java.management/sun.management=ALL-UNNAMED --add-opens jdk.management/com.sun.management.internal=ALL-UNNAMED
2022-03-24 16:23:37.032 INFO  [main] com.hazelcast.system(:56) - [10.139.221.242]:5702 [TestHazelcast] [4.2.4] Hazelcast 4.2.4 (20211220 - 25f0049) starting at [10.139.221.242]:5702
2022-03-24 16:23:37.864 INFO  [main] com.hazelcast.instance.impl.Node(:56) - [10.139.221.242]:5702 [TestHazelcast] [4.2.4] Using Discovery SPI
2022-03-24 16:23:37.883 WARN  [main] com.hazelcast.cp.CPSubsystem(:56) - [10.139.221.242]:5702 [TestHazelcast] [4.2.4] CP Subsystem is not enabled. CP data structures will operate in UNSAFE mode! Please note that UNSAFE mode will not provide strong consistency guarantees.
2022-03-24 16:23:38.502 INFO  [main] com.hazelcast.internal.diagnostics.Diagnostics(:56) - [10.139.221.242]:5702 [TestHazelcast] [4.2.4] Diagnostics disabled. To enable add -Dhazelcast.diagnostics.enabled=true to the JVM arguments.
2022-03-24 16:23:38.527 INFO  [main] com.hazelcast.core.LifecycleService(:56) - [10.139.221.242]:5702 [TestHazelcast] [4.2.4] [10.139.221.242]:5702 is STARTING

2022-03-24 16:23:39.115 ERROR [main] com.hazelcast.internal.cluster.impl.DiscoveryJoiner(:56) - [10.139.221.242]:5702 [TestHazelcast] [4.2.4] no transaction is in progress
javax.persistence.TransactionRequiredException: no transaction is in progress
        at org.hibernate.internal.AbstractSharedSessionContract.checkTransactionNeededForUpdateOperation(AbstractSharedSessionContract.java:445) ~[hibernate-core-5.6.3.Final.jar!/:5.6.3.Final]
        at org.hibernate.internal.SessionImpl.checkTransactionNeededForUpdateOperation(SessionImpl.java:3478) ~[hibernate-core-5.6.3.Final.jar!/:5.6.3.Final]
        at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1394) ~[hibernate-core-5.6.3.Final.jar!/:5.6.3.Final]
        at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1389) ~[hibernate-core-5.6.3.Final.jar!/:5.6.3.Final]
        at org.springframework.orm.hibernate5.SessionFactoryUtils.flush(SessionFactoryUtils.java:113) ~[spring-orm-5.3.14.jar!/:5.3.14]
        at org.springframework.orm.hibernate5.SpringSessionSynchronization.beforeCommit(SpringSessionSynchronization.java:95) ~[spring-orm-5.3.14.jar!/:5.3.14]
        at org.springframework.transaction.support.TransactionSynchronizationUtils.triggerBeforeCommit(TransactionSynchronizationUtils.java:97) ~[spring-tx-5.3.14.jar!/:5.3.14]
        at org.springframework.transaction.support.AbstractPlatformTransactionManager.triggerBeforeCommit(AbstractPlatformTransactionManager.java:916) ~[spring-tx-5.3.14.jar!/:5.3.14]
        at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:727) ~[spring-tx-5.3.14.jar!/:5.3.14]
        at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:711) ~[spring-tx-5.3.14.jar!/:5.3.14]
        at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:654) ~[spring-tx-5.3.14.jar!/:5.3.14]
        at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:407) ~[spring-tx-5.3.14.jar!/:5.3.14]
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) ~[spring-tx-5.3.14.jar!/:5.3.14]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.14.jar!/:5.3.14]
        at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:753) ~[spring-aop-5.3.14.jar!/:5.3.14]
        at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:698) ~[spring-aop-5.3.14.jar!/:5.3.14]
        at com.XXXX.smp.distributed.cache.db.HazelCastPingDao$$EnhancerBySpringCGLIB$$9aba005f.getMembers(<generated>) ~[HazelCastAdapter-220.0.0.0-20220322.230530-30.jar!/:?]
        at com.XXXX.smp.distributed.cache.hazelcast.HazelcastDatabaseDiscoveryStrategy.discoverNodes(HazelcastDatabaseDiscoveryStrategy.java:36) ~[HazelCastAdapter-220.0.0.0-20220322.230530-30.jar!/:?]
        at com.hazelcast.spi.discovery.impl.DefaultDiscoveryService.discoverNodes(DefaultDiscoveryService.java:72) ~[hazelcast-4.2.4.jar!/:4.2.4]
        at com.hazelcast.internal.cluster.impl.DiscoveryJoiner.getPossibleAddresses(DiscoveryJoiner.java:71) ~[hazelcast-4.2.4.jar!/:4.2.4]
        at com.hazelcast.internal.cluster.impl.DiscoveryJoiner.getPossibleAddressesForInitialJoin(DiscoveryJoiner.java:60) ~[hazelcast-4.2.4.jar!/:4.2.4]
        at com.hazelcast.internal.cluster.impl.TcpIpJoiner.joinViaPossibleMembers(TcpIpJoiner.java:136) ~[hazelcast-4.2.4.jar!/:4.2.4]
        at com.hazelcast.internal.cluster.impl.TcpIpJoiner.doJoin(TcpIpJoiner.java:96) ~[hazelcast-4.2.4.jar!/:4.2.4]
        at com.hazelcast.internal.cluster.impl.AbstractJoiner.join(AbstractJoiner.java:137) ~[hazelcast-4.2.4.jar!/:4.2.4]
        at com.hazelcast.instance.impl.Node.join(Node.java:810) ~[hazelcast-4.2.4.jar!/:4.2.4]
        at com.hazelcast.instance.impl.Node.start(Node.java:473) ~[hazelcast-4.2.4.jar!/:4.2.4]
        at com.hazelcast.instance.impl.HazelcastInstanceImpl.<init>(HazelcastInstanceImpl.java:123) ~[hazelcast-4.2.4.jar!/:4.2.4]
        at com.hazelcast.instance.impl.HazelcastInstanceFactory.constructHazelcastInstance(HazelcastInstanceFactory.java:211) ~[hazelcast-4.2.4.jar!/:4.2.4]
        at com.hazelcast.instance.impl.HazelcastInstanceFactory.newHazelcastInstance(HazelcastInstanceFactory.java:190) ~[hazelcast-4.2.4.jar!/:4.2.4]
        at com.hazelcast.instance.impl.HazelcastInstanceFactory.newHazelcastInstance(HazelcastInstanceFactory.java:128) ~[hazelcast-4.2.4.jar!/:4.2.4]
        at com.hazelcast.core.Hazelcast.newHazelcastInstance(Hazelcast.java:57) ~[hazelcast-4.2.4.jar!/:4.2.4]

HazelCastSpring配置文件

@Bean
    public HazelcastInstance hazelCastInstance() {
        Config config = config();
        config.setClusterName("TestHazelcast");
        :
        :
        HazelcastInstance hzInstance = Hazelcast.newHazelcastInstance(config);
         return hzInstance;
    }

测试应用程序

@SpringBootApplication
public class TestApplication {
    public static void main(String[] args) throws IOException {
        SpringApplication.run(TestApplication.class, args);
    }
}

数据库配置文件

@ConditionalOnProperty("spring.datasource.url")
@Configuration("databaseConfiguration")
public class DatabaseConfiguration {
    @Autowired
    private EntityManagerFactory entityManagerFactory;

    @Autowired
    ApplicationContext context;

    @PostConstruct
    public void initSessionFactory() {
        SessionFactoryImpl sessionFactory = entityManagerFactory.unwrap(SessionFactoryImpl.class);
        EventListenerRegistry registry = sessionFactory.getServiceRegistry()
            .getService(EventListenerRegistry.class);
        registry.getEventListenerGroup(EventType.PERSIST).prependListener(new EntityListener());
        registry.getEventListenerGroup(EventType.SAVE).prependListener(new EntityListener());
        registry.getEventListenerGroup(EventType.SAVE_UPDATE)
                .prependListener(new EntityListener());
        registry.getEventListenerGroup(EventType.PERSIST_ONFLUSH)
                .prependListener(new EntityListener());
        ConfigurableListableBeanFactory beanFactory =
                ((ConfigurableApplicationContext) context).getBeanFactory();
        beanFactory.registerSingleton("sessionFactory",
                sessionFactory);
    }
}

应用程序属性文件

spring.datasource.url=${datasource_url:jdbc:h2:mem:bot;DB_CLOSE_DELAY=-1;MODE=MySQL}
spring.datasource.username=${datasource_username:sa}
spring.datasource.password =${datasource_password:sa}
spring.datasource.driver-class=${datasource_driver_class:org.hibernate.dialect.H2Dialect}
spring.jpa.show-sql=${jpa_show_sql:false}

spring.jpa.hibernate.ddl-auto=update
spring.jpa.open-in-view=false

spring.datasource.hikari.idleTimeout=${datasource_idle_timeout:20000}
spring.datasource.hikari.maxLifetime=${datasource_max_lifetime:30000}
spring.datasource.hikari.connectionTimeout=${datasource_max_connection_timeout:60000}

server.port=${server_port:6080}
server.servlet.context-path=/
server.max-http-header-size=1MB

hazelcast_server_bind_group=PortalHazelcast
hibernate.allow_update_outside_transaction = true

能帮我解决这个问题吗?我已经被这个问题困扰了一个多星期了。
先谢了。

k4aesqcs

k4aesqcs1#

需要定义transactionManagerbean,请参见下面的示例:

import java.util.Properties;
import javax.sql.DataSource;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DelegatingDataSource; 
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;

/**
 *
 * @author rrodriguez
 */ 
public abstract class BasePersistenceConfig {

    protected abstract String[] getMappingResources();

    @Bean(name = "dataSource")
    public DataSource dataSource() throws Exception {
       //implementation here
    }
 
    @Bean
    public LocalSessionFactoryBean sessionFactory() throws Exception {
        LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource());
        sessionFactoryBean.setHibernateProperties(hibernateProperties());
        sessionFactoryBean.setMappingResources(getMappingResources());

        return sessionFactoryBean;
    }

    @Autowired
    @Bean(name = "transactionManager")
    public HibernateTransactionManager getTransactionManager(
            SessionFactory sessionFactory) { 
        return new HibernateTransactionManager(sessionFactory);
    }

    Properties hibernateProperties() {
        return new Properties() {
            {
                setProperty("hibernate.hbm2ddl.auto", "update");
                setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect"); 
            }
        };
    }

}

相关问题