配置cloud-sql项目时是否会出现java.lang.reflect.InvocationTargetException?

时间:2018-12-24 05:51:50

标签: java eclipse google-cloud-platform google-cloud-sql

我正在使用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:\Users\Infiflex52\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp3\project_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)

1 个答案:

答案 0 :(得分:0)

通过将connectionURL设置为 jdbc:mysql:// localhost:3306 / giffy_db ,可以解决此问题,这里的giffy_db是我在工作台工具上创建的本地mysql数据库,而3306是默认端口。此外,不推荐使用connectionDriverName ,因此必须使用更高版本的 com.mysql.cj.jdbc.Driver 进行更改。并且必须在 spring-boot-starter-2.0.5.RELEASE

的类路径中添加一个jar