0

我在 karaf 2.2.10 之上使用带有 datanucleus-mongodb 3.2.3 和 spring 3.0.7 的 JDO。

在新的 OSGi 捆绑安装中,我可以毫无问题地在 mongodb 上坚持,但是在重新安装捆绑后,我在构建新的 JDO PersistenceManagerFactory 时收到以下错误:

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

其中 ID (242) 是指第一个分配给包的 ID(换句话说,旧包的 ID)。

前一个包的上下文似乎已正确关闭,“旧”JDO PersistenceManagerFactory 也是如此。

我的本地持久性管理器工厂 bean 类(基于dn 站点中提供的示例):

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;
    }
}

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

<!-- 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 />

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

任何提示将不胜感激!

4

2 回答 2

2

确保您还刷新了依赖包,因为这些包通常坚持旧的卸载包。在清除所有引用之前,它仍然存在。在 Karaf shell 中,您只需要在没有 ID 的情况下发出刷新或简单刷新,这将导致整个容器重新连接 :)

于 2013-10-23T11:32:49.500 回答
0

对于那些不能简单刷新的人,javax.jdo请确保在重新ClassLoader创建PersistenceManagerFactory. 如果没有改变Datanucleus,将不会尝试加载相同的类两次。ClassLoader你最终会ClassCastExceptionsEntity.classcannot be cast to Entity.class

于 2017-08-22T17:46:48.343 回答