我正在使用JDO将Java appengine应用程序与Cloud SQL集成。但是,在开发服务器上运行应用程序时出现以下错误。为了连接云sql,我们修改了PMF.java。
PMF.java
public class PMF{
private static PersistenceManagerFactory PMF;
private static final ThreadLocal<PersistenceManager> PER_THREAD_PM = new
ThreadLocal<PersistenceManager>();
private PMF() {}
public static void initialize() {
Map<String, String> properties = new HashMap();
if (SystemProperty.environment.value() ==
SystemProperty.Environment.Value.Production) {
System.err.println("Enviroment " + SystemProperty.environment.value() );
/*properties.put("javax.jdo.option.ConnectionDriverName",
"com.mysql.jdbc.GoogleDriver");*/
properties.put("javax.jdo.option.ConnectionDriverName",
"com.mysql.jdbc.Driver");
properties.put("javax.jdo.option.ConnectionURL",
System.getProperty("jdbc:mysql://localhost:8080/cloud_sqldatabase"));
properties.put("javax.jdo.option.ConnectionUserName", "root");
properties.put("javax.jdo.option.ConnectionPassword", "#pass12341992");
// properties.put("datanucleus.autoCreateSchema", "true");
properties.put("javax.jdo.PersistenceManagerFactoryClass", "org.datanucleus.api.jdo.JDOPersistenceManagerFactory");
} else {
properties.put("javax.jdo.option.ConnectionDriverName",
"com.mysql.jdbc.Driver");
properties.put("javax.jdo.option.ConnectionURL",
System.getProperty("jdbc:mysql://localhost:8080/cloud_sqldatabase"));
properties.put("javax.jdo.option.ConnectionUserName", "root");
properties.put("javax.jdo.option.ConnectionPassword", "#pass12341992");
//properties.put("datanucleus.autoCreateSchema", "true");
properties.put("javax.jdo.PersistenceManagerFactoryClass", "org.datanucleus.api.jdo.JDOPersistenceManagerFactory");
}
if (PMF != null) {
return;
//throw new IllegalStateException("initialize() already called");
}
PMF = JDOHelper.getPersistenceManagerFactory(properties,"transactions-optional");
}
public static PersistenceManager getPersistenceManager() {
PersistenceManager pm = PER_THREAD_PM.get();
if (pm == null) {
pm = getPMF().getPersistenceManager();
PER_THREAD_PM.set(pm);
}
return pm;
}
public static void finishRequest() {
PersistenceManager pm = PER_THREAD_PM.get();
if (pm != null) {
PER_THREAD_PM.remove();
Transaction tx = pm.currentTransaction();
if (tx.isActive()) {
tx.rollback();
}
if(!pm.isClosed()){
pm.close();
}
}
}
public static PersistenceManagerFactory getPMF() {
Map<String, String> properties = new HashMap();
if (SystemProperty.environment.value() ==
SystemProperty.Environment.Value.Production) {
/*properties.put("javax.jdo.option.ConnectionDriverName",
"com.mysql.jdbc.GoogleDriver");*/
properties.put("javax.jdo.option.ConnectionDriverName",
"com.mysql.jdbc.Driver");
properties.put("javax.jdo.option.ConnectionURL",
System.getProperty("jdbc:mysql://localhost:8080/cloud_sqldatabase"));
properties.put("javax.jdo.option.ConnectionUserName", "root");
properties.put("javax.jdo.option.ConnectionPassword", "#pass12341992");
// properties.put("datanucleus.autoCreateSchema", "true");
properties.put("javax.jdo.PersistenceManagerFactoryClass", "org.datanucleus.api.jdo.JDOPersistenceManagerFactory");
} else {
properties.put("javax.jdo.option.ConnectionDriverName",
"com.mysql.jdbc.Driver");
properties.put("javax.jdo.option.ConnectionURL",
System.getProperty("jdbc:mysql://localhost:8080/cloud_sqldatabase"));
properties.put("javax.jdo.option.ConnectionUserName", "<>");
properties.put("javax.jdo.option.ConnectionPassword", "<>");
//properties.put("datanucleus.autoCreateSchema", "true");
properties.put("javax.jdo.PersistenceManagerFactoryClass", "org.datanucleus.api.jdo.JDOPersistenceManagerFactory");
}
if (PMF == null) {
PMF = JDOHelper
.getPersistenceManagerFactory(properties,"transactions-optional");
}
return PMF;
}
}
错误
NestedThrowables:
java.lang.reflect.InvocationTargetException
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:162)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:990)
... 47 more
Caused by: javax.jdo.JDOFatalInternalException: Error creating transactional connection factory
NestedThrowables:
java.lang.reflect.InvocationTargetException
at org.datanucleus.api.jdo.NucleusJDOHelper.getJDOExceptionForNucleusException(NucleusJDOHelper.java:587)
at org.datanucleus.api.jdo.JDOPersistenceManagerFactory.freezeConfiguration(JDOPersistenceManagerFactory.java:788)
at org.datanucleus.api.jdo.JDOPersistenceManagerFactory.createPersistenceManagerFactory(JDOPersistenceManagerFactory.java:333)
at org.datanucleus.api.jdo.JDOPersistenceManagerFactory.getPersistenceManagerFactory(JDOPersistenceManagerFactory.java:263)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at javax.jdo.JDOHelper$16.run(JDOHelper.java:1965)
at java.security.AccessController.doPrivileged(Native Method)
at javax.jdo.JDOHelper.invoke(JDOHelper.java:1960)
at javax.jdo.JDOHelper.invokeGetPersistenceManagerFactoryOnImplementation(JDOHelper.java:1128)
at javax.jdo.JDOHelper.getPersistenceManagerFactory(JDOHelper.java:808)
at javax.jdo.JDOHelper.getPersistenceManagerFactory(JDOHelper.java:1093)
at javax.jdo.JDOHelper.getPersistenceManagerFactory(JDOHelper.java:960)
at net.giffy.jdo.PMF.getPMF(PMF.java:107)
at net.giffy.jdo.PMF.getPersistenceManager(PMF.java:60)
at net.giffy.service.ApplicationService.getApplication(ApplicationService.java:203)
at net.giffy.service.EntityService.<init>(EntityService.java:98)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
... 49 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.datanucleus.plugin.NonManagedPluginRegistry.createExecutableExtension(NonManagedPluginRegistry.java:681)
at org.datanucleus.plugin.PluginManager.createExecutableExtension(PluginManager.java:325)
at org.datanucleus.store.AbstractStoreManager.registerConnectionFactory(AbstractStoreManager.java:287)
at org.datanucleus.store.AbstractStoreManager.<init>(AbstractStoreManager.java:251)
at org.datanucleus.store.mapped.MappedStoreManager.<init>(MappedStoreManager.java:133)
at org.datanucleus.store.rdbms.RDBMSStoreManager.<init>(RDBMSStoreManager.java:239)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.datanucleus.plugin.NonManagedPluginRegistry.createExecutableExtension(NonManagedPluginRegistry.java:681)
at org.datanucleus.plugin.PluginManager.createExecutableExtension(PluginManager.java:301)
at org.datanucleus.NucleusContext.createStoreManagerForProperties(NucleusContext.java:476)
at org.datanucleus.NucleusContext.initialise(NucleusContext.java:288)
at org.datanucleus.api.jdo.JDOPersistenceManagerFactory.freezeConfiguration(JDOPersistenceManagerFactory.java:775)
... 71 more
Caused by: org.datanucleus.exceptions.NucleusUserException: Unable to create transactional datasource for connections due to invalid/insufficient input. Consult the log for details and/or review the settings of "datastore.connectionXXX" properties
at org.datanucleus.store.rdbms.ConnectionFactoryImpl.<init>(ConnectionFactoryImpl.java:98)
... 90 more
2018-12-24 12:33:28.235:WARN:oejw.WebAppContext:main: Failed startup of context c.g.a.t.d.j.DevAppEngineWebAppContext@30b975ad{/,file:///C:/Users/Infiflex52/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp3/project_sql/,UNAVAILABLE}{C:UsersInfiflex52workspace.metadata.pluginsorg.eclipse.wst.server.coretmp3project_sql}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityService' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [net.giffy.service.EntityService]: Constructor threw exception; nested exception is javax.jdo.JDOFatalInternalException: Error creating transactional connection factory|NestedThrowables:|java.lang.reflect.InvocationTargetException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:997)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:943)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:925)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:472)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:388)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:293)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:952)
at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:558)
at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:917)
at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:370)
at org.eclipse.jetty.webapp.WebAppContext.startWebapp(WebAppContext.java:1497)
at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1459)
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:847)
at org.eclipse.jetty.servlet.ServletContextHandler.doStart(ServletContextHandler.java:287)
at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:545)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:138)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:108)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:138)
at org.eclipse.jetty.server.Server.start(Server.java:416)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:108)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113)
at org.eclipse.jetty.server.Server.doStart(Server.java:383)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at com.google.appengine.tools.development.jetty9.JettyContainerService.startContainer(JettyContainerService.java:343)
at com.google.appengine.tools.development.AbstractContainerService.startup(AbstractContainerService.java:284)
at com.google.appengine.tools.development.AbstractBackendServers$ServerWrapper.startup(AbstractBackendServers.java:729)
at com.google.appengine.tools.development.AbstractBackendServers.startupAll(AbstractBackendServers.java:266)
at com.google.appengine.tools.development.DevAppServerImpl.doStart(DevAppServerImpl.java:293)
at com.google.appengine.tools.development.DevAppServerImpl.access$000(DevAppServerImpl.java:47)
at com.google.appengine.tools.development.DevAppServerImpl$1.run(DevAppServerImpl.java:223)
at com.google.appengine.tools.development.DevAppServerImpl$1.run(DevAppServerImpl.java:221)
at java.security.AccessController.doPrivileged(Native Method)
at com.google.appengine.tools.development.DevAppServerImpl.start(DevAppServerImpl.java:221)
at com.google.appengine.tools.development.DevAppServerMain$StartAction.apply(DevAppServerMain.java:395)
at com.google.appengine.tools.util.Parser$ParseResult.applyArgs(Parser.java:45)
at com.google.appengine.tools.development.DevAppServerMain.run(DevAppServerMain.java:247)
at com.google.appengine.tools.development.DevAppServerMain.main(DevAppServerMain.java:238)
通过将连接URL作为jdbc:mysql://localhost:3306/giffy_db来解决此问题,这里giffy_db是我的本地mysql数据库,该数据库已在工作台工具上创建,3306是默认端口。此外,connectionDriverName com.mysql.jdbc.Driver 已被弃用,因此不得不使用较新版本 com.mysql.cj.jdbc.Driver 进行更改,并且必须在类路径中添加一个 jar,即 spring-boot-starter-2.0.5.RELEASE