本文介绍了jdo/OSGi:捆绑更新后构建JDO PersistenceManagerFactory时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将JDO与datanucleus-mongodb 3.2.3和spring 3.0.7一起使用在Karaf 2.2.10之上.

I'm using JDO with datanucleus-mongodb 3.2.3 and spring 3.0.7 on top of karaf 2.2.10.

在全新安装的OSGi捆绑包上,我可以在mongodb上继续使用而不会出现问题,但是在重新安装捆绑包后,在构建新的JDO PersistenceManagerFactory时出现以下错误:

On a fresh OSGi bundle install I'm able to persist on mongodb without issues, but after a bundle reinstall, I get the following error while building a new JDO PersistenceManagerFactory:

ERROR: Bundle [my_bundle] [242] Unable to get module class path.
(java.lang.IllegalStateException: zip file closed)      

其中ID(242)指的是捆绑软件中第一个分配的ID(换句话说,是旧捆绑软件的ID).

Where the ID (242) refers to the first assigned ID to the bundle (in other words, the ID of the old bundle).

以前的捆绑软件以及旧的" JDO PersistenceManagerFactory的上下文似乎都已正确关闭.

The context of the previous bundle seems to be properly closed, as well as the "old" JDO PersistenceManagerFactory.

我的本​​地持久性管理器工厂bean类(基于示例):

My local persistence manager factory bean class (based on the example provided in the dn site):

public class OSGiLocalPersistenceManagerFactoryBean
    extends LocalPersistenceManagerFactoryBean implements BundleContextAware {

    public static final String JDO_BUNDLE_NAME    = "org.datanucleus.api.jdo";
    public static final String JDO_PMF_CLASS_NAME = "org.datanucleus.api.jdo.JDOPersistenceManagerFactory";

    private BundleContext bundleContext;

    @Override
    protected PersistenceManagerFactory newPersistenceManagerFactory(String name) {
        return JDOHelper.getPersistenceManagerFactory(name, getClassLoader());
    }

    @Override
    protected PersistenceManagerFactory newPersistenceManagerFactory(Map props) {
        ClassLoader classLoader = getClassLoader();

        props.put("datanucleus.primaryClassLoader", classLoader);

        if (FrameworkUtil.getBundle(this.getClass()) != null) { // running in OSGi
            props.put("datanucleus.plugin.pluginRegistryClassName", "org.datanucleus.plugin.OSGiPluginRegistry");
        }

        PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(props, classLoader);

        return pmf;
    }

    private ClassLoader getClassLoader() {
        ClassLoader classLoader = null;
        Bundle thisBundle = FrameworkUtil.getBundle(this.getClass());

        if (thisBundle != null) { // on OSGi runtime
            Bundle[] bundles = bundleContext.getBundles();

            for (Bundle bundle : bundles) {
                if (JDO_BUNDLE_NAME.equals(bundle.getSymbolicName())) {
                    try {
                        classLoader = bundle.loadClass(JDO_PMF_CLASS_NAME).getClassLoader();
                    } catch (ClassNotFoundException e) {
                        // do something fancy here ...
                    }
                    break;
                }
            }
        } else { // somewhere else
            classLoader = this.getClass().getClassLoader();
        }
        return classLoader;
    }

    @Override
    public void setBundleContext(BundleContext bundleContext) {
        this.bundleContext = bundleContext;
    }
}

上下文中的一些与持久性相关的摘录:

Some persistence related excerpts from the context:

<!-- persistence manager factory -->
<bean id="pmf" class="[my_package].OSGiLocalPersistenceManagerFactoryBean">
    <property name="configLocation" value="classpath:datanucleus.properties" />
</bean>

<!-- persistence manager factory proxy -->
<bean id="persistenceManagerFactoryProxy"
      class="org.springframework.orm.jdo.TransactionAwarePersistenceManagerFactoryProxy">
    <property name="targetPersistenceManagerFactory" ref="pmf" />
</bean>

<!-- transactions -->
<bean id="transactionManager" class="org.springframework.orm.jdo.JdoTransactionManager">
    <property name="persistenceManagerFactory" ref="persistenceManagerFactoryProxy" />
</bean>

<tx:annotation-driven />

错误消息似乎指向类加载问题,但是,为什么它仅在重新部署期间失败?

The error message seems to point to a classloading issue, but then, why does it fail only during re-deployment?

任何提示将不胜感激!

推荐答案

请确保您还刷新了依赖的软件包,因为这些软件包通常会粘贴到旧的已卸载软件包中.在清除所有引用之前,它仍然存在.在Karaf Shell中,您只需要发出刷新或不带ID的简单刷新,这将导致整个容器重新布线:)

Make sure you also refresh the depending bundles, cause those usually stick to the old uninstalled bundle. It's still there until all references are cleared. In the Karaf shell you'll just need to issue refresh or simple refresh without the ID, this will cause the complete container to do a re-wiring :)

这篇关于jdo/OSGi:捆绑更新后构建JDO PersistenceManagerFactory时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 17:27