春季,野蝇,休眠-拥有者会议已关闭

时间:2018-12-27 08:07:15

标签: java spring hibernate jpa wildfly

几年来,我们已经在生产中使用了现有的功能齐全的Web解决方案。关键技术是

野生蝇10.1.0 Java 8 春季4.3.3 Hibernate 5.0.10(Wildfly 10.1.0中的JPA引擎) 启用5.0.10

所以升级要转到设置

野生蝇15.0 Java 11 春季5.1.3 Hibernate 5.3.7(Wildfly 15中的JPA引擎)

只需执行此开关,即可对次要代码进行少量更改(非常次要),一切正常,应用程序启动,我可以毫无问题地登录,即,我可以毫无问题地读取并映射到实体。但是在更新@Transaction下的数据库时,我们遇到了一个很大的问题,该问题似乎与会话提前关闭有关(类似于Hibernate bug HHH-11570的相同讨论)。更新似乎进行得很顺利,这是当Hibernate需要为延迟加载实体启动会话时,该会话关闭时,Hibernate引发异常。 请让我知道这是一个错误吗?我试图将原始的未升级的Java 8 war部署到服务器,但是问题是完全相同的,因此这是一个很好的论据,可以说问题是Hibernate或Wildfly。我真的需要升级系统。让我知道是否需要更多的堆栈跟踪,代码或配置。

Spring的一些配置是

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages="se.eaktiebok.repository")
public class JpaConfiguration {
    @Bean
    public DataSource dataSource() throws NamingException{
        Context ctx = new InitialContext();
        DataSource dataSource = (DataSource)ctx.lookup("java:/eaktiebok");
        return dataSource;
    }   

@Bean
public PlatformTransactionManager transactionManager() throws NamingException{
    JtaTransactionManager tm = new JtaTransactionManager();
    tm.afterPropertiesSet();
    return tm;
}

@Bean
public SharedEntityManagerBean entityManager() throws NamingException{
    SharedEntityManagerBean entityManager = new SharedEntityManagerBean();
    entityManager.setEntityManagerFactory(this.entityManagerFactory());
    return entityManager;
}   

@Bean 
EntityManagerFactory entityManagerFactory() throws IllegalArgumentException, NamingException{
    org.springframework.jndi.JndiObjectFactoryBean jndiObjectFactoryBean = new org.springframework.jndi.JndiObjectFactoryBean();
    jndiObjectFactoryBean.setJndiName("java:app/JPADBFactory");
    jndiObjectFactoryBean.setLookupOnStartup(true);
    jndiObjectFactoryBean.setExpectedType(EntityManagerFactory.class);
    jndiObjectFactoryBean.afterPropertiesSet(); 
    return (EntityManagerFactory) jndiObjectFactoryBean.getObject();
}

}

AuthUser实体

@Entity
@Table(name=“auth_user”)
@Audited(withModifiedFlag=true)
public class AuthUser implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer iduser;

....

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="idclient", insertable=false, updatable = false)
private AuthClient authClient;
}

wildfly数据源在standalone.xml中定义为

    <datasource jndi-name="java:/eaktiebok" pool-name="eaktiebok">
        <connection-url>jdbc:mysql://xxxxxxxxxxxxxxxxx:3306/eaktiebokdump?useSSL=false</connection-url>
        <driver-class>com.mysql.jdbc.Driver</driver-class>
        <driver>mysql</driver>
        <security>
            <user-name>xxxxxxx</user-name>
            <password>xxxxxxx</password>
        </security>
        <validation>
            <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/>
            <background-validation>true</background-validation>
            <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/>
        </validation>
    </datasource>

persistence.xml

<?xml version="1.0" encoding="UTF-8" ?>
<persistence-unit name="jpaEabMysqlUnit" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:/eaktiebok</jta-data-source>
    <properties>
        <property name="hibernate.jdbc.use_streams_for_binary" value="true"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
        <property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
        <property name="org.hibernate.envers.store_data_at_delete" value="false"/>
        <property name="hibernate.generate_statistics" value="false"/>
        <property name="jboss.entity.manager.jndi.name" value="java:app/JPADB"/>
        <property name="jboss.entity.manager.factory.jndi.name" value="java:app/JPADBFactory"/>
    </properties>
</persistence-unit>

最后是控制台的一小部分带有一些堆栈跟踪

09:05:39,320 DEBUG [org.springframework.transaction.jta.JtaTransactionManager.handleExistingTransaction] (default task-9) Participating in existing transaction
09:05:39,320 DEBUG [org.springframework.orm.jpa.EntityManagerFactoryUtils.closeEntityManager] (default task-9) Closing JPA EntityManager
09:05:39,321 DEBUG [org.springframework.transaction.jta.JtaTransactionManager.processCommit] (default task-9) Initiating transaction commit
09:05:39,321 DEBUG [org.hibernate.event.internal.AbstractFlushingEventListener.prepareEntityFlushes] (default task-9) Processing flush-time cascades
09:05:39,321 DEBUG [org.hibernate.event.internal.AbstractFlushingEventListener.prepareCollectionFlushes] (default task-9) Dirty checking collections


Caused by: org.hibernate.LazyInitializationException: could not initialize proxy [se.eaktiebok.jpa.auth.AuthClient#1] - the owning Session was closed
    at org.hibernate@5.3.7.Final//org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:172)
    at org.hibernate@5.3.7.Final//org.hibernate.proxy.AbstractLazyInitializer.getIdentifier(AbstractLazyInitializer.java:89)
    at org.hibernate@5.3.7.Final//org.hibernate.envers.internal.entities.mapper.id.SingleIdMapper.mapToMapFromEntity(SingleIdMapper.java:125)
    at org.hibernate@5.3.7.Final//org.hibernate.envers.internal.entities.mapper.relation.ToOneIdMapper.mapToMapFromEntity(ToOneIdMapper.java:55)
    at org.hibernate@5.3.7.Final//org.hibernate.envers.internal.entities.mapper.MultiPropertyMapper.map(MultiPropertyMapper.java:90)
    at org.hibernate@5.3.7.Final//org.hibernate.envers.internal.synchronization.work.ModWorkUnit.<init>(ModWorkUnit.java:43)
    at org.hibernate@5.3.7.Final//org.hibernate.envers.event.spi.EnversPostUpdateEventListenerImpl.onPostUpdate(EnversPostUpdateEventListenerImpl.java:46)
    at org.hibernate@5.3.7.Final//org.hibernate.action.internal.EntityUpdateAction.postUpdate(EntityUpdateAction.java:268)
    at org.hibernate@5.3.7.Final//org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:215)
    at org.hibernate@5.3.7.Final//org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:604)
    at org.hibernate@5.3.7.Final//org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:478)
    at org.hibernate@5.3.7.Final//org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:356)
    at org.hibernate@5.3.7.Final//org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
    at org.hibernate@5.3.7.Final//org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1454)
    ... 131 more

完成开始交易的业务功能

@Override
@Transactional
public UserBean updateIntoUser(UserBean userBean, UserBean authenticatedUser) throws AuthUserException {
    Assert.notNull(userBean, "userBean must not be null");
    Assert.notNull(authenticatedUser, "authenticatedUser must not be null");

    // If identity is set, check so no other user has it
    if(userBean.getIdentity()!=null) {
        UserBean ub = findAuthUserByIdentity(userBean.getIdentity());
        if(ub!=null) {
            if(!ub.getIduser().equals(userBean.getIduser())) {
                throw new AuthUserException("Contact/Company taken by iduser "+ub.getIduser(), AuthUserExceptionCause.contactTaken);
            }
        }

        OwnerEntityBean<?> o = companyDao.findOwnerEntityBean(userBean.getIdentity());
        UserEntity ue = null;
        if(o!=null) {
            ue = new UserEntity();
            ue.setAddressBean(o.getAddress());
            ue.setEntityType(o.getEntityType());
            ue.setIdentityNumber(o.getIdentityNumber());
            ue.setName(o.getName());
        }
        userBean.setUserEntity(ue);

    }
    // If username changed check so it is free
    if(!authenticatedUser.getUsername().equals(userBean.getUsername())) {
        AuthUser user = userRepo.findUserByUsername(userBean.getUsername());
        if(user!=null&&!user.getIduser().equals(userBean.getIduser())) {
            throw new AuthUserException("Username already exists "+userBean.toString(), AuthUserExceptionCause.nonUniqueUsername);
        }
    }

    // update AuthUser 
    userDao.updateIntoUser(userBean);

    // Load AuthUser with contact and address

    return userBean;
} // end function updateIntoUser

0 个答案:

没有答案