Wildfly 10,Spring,Hibernate 4.2 - 事务配置

时间:2016-10-17 23:43:15

标签: java spring hibernate wildfly-10

我无法弄清楚如何正确配置与Wildfly和Hibernate一起使用的事务管理器。我的配置是:

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
   <property name="dataSource" ref="dataSource"/>
   <property name="hibernateProperties">
   <!-- All properties provided by application server environment -->
   <props>
   <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
   <prop key="hibernate.hbm2ddl.auto">validate</prop>
   <!-- Second level cache configuration -->
   <prop key="hibernate.cache.use_second_level_cache">false</prop>
   <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
   <prop key="hibernate.generate_statistics">true</prop>
   <prop key="hibernate.cache.use_structured_entries">true
   </prop>
   <!-- Query cache -->
   <prop key="hibernate.cache.use_query_cache">false</prop>
   <prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</prop>
   <prop key="hibernate.cache.provider_configuration_file_resource_path">
  ""
   </prop>
   <prop key="jadira.usertype.autoRegisterUserTypes">true</prop>
   <prop key="jadira.usertype.databaseZone">jvm</prop>
   <prop key="jadira.usertype.javaZone">jvm</prop>
   <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</prop>
   <prop key="hibernate.transaction.manager_lookup_class">
  org.hibernate.transaction.JBossTransactionManagerLookup
  </prop>
   <prop key="hibernate.transaction.jta.platform">jta</prop>
   </props>
   </property>
   <property name="packagesToScan" value="org.example"/>
</bean>
<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager">
   <property name="allowCustomIsolationLevels" value="true"/>
</bean>

我想要实现的是使用相同的事务让事务服务bean包含另外两个具有数据库访问权限的bean。

@Service("terminalQueryService")
public class UserQueryServiceImpl implements UserQueryService {

    @Transactional(readOnly = true, propagation = Propagation.REQUIRED)
    public PaginatedList<PetDto> browse(PageDefinition pageDefinition, String userLogin) {
        User user = null;
        try {
            user = userQueriesFacade.getUser(userLogin);
        } catch (AuthorizationServiceException e) {
            return PaginatedList.getEmptyList();
        }
        PaginatedList<Pet> petListagedList = petQueriesFacade.browse(
                pageDefinition, user.getId());

        List<PetDto> petDtoList = new ArrayList<>();
        List<Pet> petEntityList = petEntityPagedList.getList();
        dtoMapper.assembleDtos("petDtoKey", petDtoList, petEntityList);
        return petEntityPagedList.transform(petDtoList);
    }
}

查询fasades看起来像:

@Cacheable("userForSecurity")
@Transactional(propagation = Propagation.REQUIRED)
public User getUser(PageDefinition pageDefinition, String login) {

    User user = (User) getSession()
            .createQuery(
                    "from User mu left outer join fetch mu.additionalPrivileges left outer join fetch mu.roles where mu.login = :login AND mu.status != :archivedStatus")
            .setParameter("login", login)
            .setParameter("archivedStatus", UserStatus.ARCHIVED)
            .uniqueResult();
    return user;
}

我得到的错误是

WARN  [org.springframework.remoting.support.RemoteInvocationTraceInterceptor] (default task-38) Processing of HttpInvokerServiceExporter remote call resulted in fatal exception: org.example.logic.remote.services.user.UserQueryService.browse: org.hibernate.LazyInitializationException: could not initialize proxy - no Session

它尝试转换的行dto表明会话已关闭。

如果我从fasades中移除@Transactional然后(并将其保留在服务上)第一个查询执行没有任何问题,第二个引发:

[org.springframework.remoting.support.RemoteInvocationTraceInterceptor] (default task-37) Processing of HttpInvokerServiceExporter remote call resulted in fatal exception: org.example.logic.remote.services.user.UserQueryService.browse: org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134) at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:988)

任何想法?

0 个答案:

没有答案