Tapestry覆盖HibernateSessionManager错误

时间:2013-09-12 20:33:11

标签: hibernate tapestry

我正在尝试覆盖org.apache.tapestry5.hibernate.HibernateSessionManager。为此,我将其添加到我的AppModule:

public static void contributeServiceOverride(
        MappedConfiguration<Class<?>, Object> configuration)
{
    configuration.addInstance(HibernateSessionManager.class,
            HibernateSessionManagerOverride.class);
}

HibernateSessionManagerOverride(目前)的内容与默认的Tapestry实施org.apache.tapestry5.internal.hibernate.HibernateSessionManagerImpl相同。

当我运行我的应用程序时,我收到此错误:

java.lang.RuntimeException: 
Exception constructing service 'ServletApplicationInitializer': 
Unable to instantiate class org.apache.tapestry5.services.TapestryModule as a module: 
Exception constructing service 'ServiceOverride': 
Error invoking service contribution method com.company.services.AppModule.contributeServiceOverride(MappedConfiguration): 
Exception constructing service 'ServiceOverride': 
Construction of service 'ServiceOverride' has failed due to recursion:
the service depends on itself in some way. Please check org.apache.tapestry5.ioc.internal.services.ServiceOverrideImpl(Map) (at ServiceOverrideImpl.java:31) via org.apache.tapestry5.ioc.services.TapestryIOCModule.bind(ServiceBinder) (at TapestryIOCModule.java:49) for references to another service that is itself dependent on service 'ServiceOverride'.

2 个答案:

答案 0 :(得分:1)

查看hibernateModule.java,我可以看到以下内容:

@Contribute(ServiceOverride.class)
public static void provideInjectableSessionObject(
        MappedConfiguration<Class, Object> configuration, 
        @HibernateCore Session session)
{
    configuration.add(Session.class, session);
}

也许这会导致循环依赖?

您可能需要使用decorator而不是ServiceOverride

例如:

public static HibernateSessionManager decorateHibernateSessionManager(
        HibernateSessionManager defaultManager, 
        @Autobuild HibernateSessionManagerOverride overrideManager)
{
    return overrideManager;
}

答案 1 :(得分:0)

我不想让Lance的答案贬值,但是使用这种方法我发现没有添加或调用ThreadCleanupListener,这意味着在线程生命周期结束时没有回滚会话。我修改了上面的例子,将override添加为threadcleanup监听器。

public static HibernateSessionManager decorateHibernateSessionManager(
        HibernateSessionManager defaultManager,
        @Autobuild HibernateSessionManagerOverride overrideManager,
        PerthreadManager perthreadManager)
{
    perthreadManager.addThreadCleanupListener(overrideManager);
    return overrideManager;
}

这非常关键,以便会话在使用后回滚和关闭。