我正在尝试在 osgi 环境中使用 datanucleus jdo 实现,但我不断收到错误消息:找不到适合 jdbc:mysql://localhost:3306/jdoosgitest 的驱动程序
我一直在关注链接 http://www.datanucleus.org/servlet/wiki/display/USERS/HOWTO+Use+Datanucleus+with+OSGi+and+Spring+DM
但我不想使用 SpringDM。首先是因为我不了解 Spring,而且我也是 OSGi 和 JDO 的新手。
环境细节
MANIFEST.MF 中的 DataNucleus 捆绑包和更改
datanucleus-api-jdo-3.0.0-m2.jar -- Eclipse-BuddyPolicy=已注册
datanucleus-core-3.0.0-m2.jar -- Eclipse-BuddyPolicy=已注册
datanucleus-rdbms-3.0.0-m2.jar -- Eclipse-BuddyPolicy=已注册
jdo-api-3.0.jar -- Eclipse-BuddyPolicy=已注册
Eclipse Bundle 和 Equinox
org.eclipse.core.contenttype_3.4.1.R35x_v20090826-0451.jar org.eclipse.core.jobs_3.4.100.v20090429-1800.jar org.eclipse.core.runtime_3.5.0.v20090525.jar org.eclipse.equinox.app_1 .2.1.R35x_v20091203.jar org.eclipse.equinox.common_3.5.1.R35x_v20090807-1100.jar org.eclipse.equinox.preferences_3.2.301.R35x_v20091117.jar org.eclipse.equinox.registry_3.4.101800-v20090520-jar .eclipse.osgi_3.5.2.R35x_v20100126.jar org.eclipse.osgi.services_3.2.0.v20090520-1800.jar
MyBundle: TestJdoOSGi
使用的其他库 TestJdoOSGi/lib/mysql-connector-java-5.1.14-bin.jar
TestJdoOSGi/META-INF/MANIFEST.MF --
Eclipse-RegisterBuddy= org.datanucleus.store.rdbms, javax.jdo, org.datanucleus.api.jdo, org.datanucleus
Bundle-ClassPath= bin, lib/mysql-connector-java-5.1.14-bin.jar
获取 PersistenceManager 工厂的方法
public PersistenceManagerFactory createPMF()
{
Map defaultProps = new HashMap();
defaultProps.put("javax.jdo.PersistenceManagerFactoryClass", "org.datanucleus.api.jdo.JDOPersistenceManagerFactory");
defaultProps.put("datanucleus.metadata.validate", "false");
defaultProps.put("javax.jdo.option.ConnectionDriverName", "org.gjt.mm.mysql.Driver");
defaultProps.put("javax.jdo.option.ConnectionURL", "jdbc:mysql://localhost:3306/jdoosgitest");
defaultProps.put("javax.jdo.option.ConnectionUserName", "root");
defaultProps.put("javax.jdo.option.ConnectionPassword", "root");
defaultProps.put("javax.jdo.option.Mapping", "mysql");
defaultProps.put("datanucleus.autoCreateSchema", "true");
defaultProps.put("datanucleus.validateTables", "false");
defaultProps.put("datanucleus.validateConstraints", "false");
ClassLoader dirverClassLoader = null;
try {
dirverClassLoader = Activator.bundleContext.getBundle().loadClass("test.jdo.osgi.MyTest").getClassLoader();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
defaultProps.put("datanucleus.primaryClassLoader", dirverClassLoader);
ClassLoader factoryClassLoader = getFactoryClassLoader();
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(defaultProps, factoryClassLoader);
return pmf;
}
private ClassLoader getFactoryClassLoader() {
ClassLoader classloader = null;
Bundle[] bundles = Activator.bundleContext.getBundles();
for (int x = 0; x < bundles.length; x++) {
if ("org.datanucleus.api.jdo".equals(bundles[x].getSymbolicName())) {
try {
classloader = bundles[x].loadClass("org.datanucleus.api.jdo.JDOPersistenceManagerFactory").getClassLoader();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
return classloader;
}