【问题标题】:Spring boot Google SQL is not working properlySpring Boot Google SQL 无法正常工作
【发布时间】:2018-09-23 15:08:03
【问题描述】:

我开发了一个 spring boot 应用程序,可以在带有 google sql 的 google app 引擎上运行,并且可以在本地完美运行。当我部署应用程序和其余 url 时,它工作了一段时间,一个小时后我得到以下信息。将代码部署到谷歌应用引擎后,我能够从数据库中获取记录,并且在一小时后不获取记录。我正在使用 Spring Boot 版本 2.0.0.RELEASE 和 java 8

错误

org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution

这些是我正在使用的配置 POM.XML

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.6.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.google.cloud.sql/mysql-socket-factory -->
        <dependency>
            <groupId>com.google.cloud.sql</groupId>
            <artifactId>mysql-socket-factory</artifactId>
            <version>1.0.5</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/commons-net/commons-net -->
        <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>3.6</version>
        </dependency>
        <dependency>
            <groupId>com.google.api-client</groupId>
            <artifactId>google-api-client</artifactId>
            <version>1.21.0</version>
        </dependency>

    </dependencies>

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.0.0.RELEASE</version>
            </plugin>
            <!-- [START cloudplugin] -->
            <plugin>
                <groupId>com.google.cloud.tools</groupId>
                <artifactId>appengine-maven-plugin</artifactId>
                <version>1.3.1</version>
            </plugin>
            <!-- [END cloudplugin] -->

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>versions-maven-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>display-dependency-updates</goal>
                            <goal>display-plugin-updates</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <excludes>
                        <exclude>javax.servlet:javax.servlet-api</exclude>
                        <exclude>com.google.guava:guava</exclude> <!-- avoid android version -->
                    </excludes>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>

        </plugins>
    </build>

application.properties

spring.datasource.url=jdbc:mysql://google/fbconferencebot?cloudSqlInstance=conference:us-central1:appconference&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=root&password=*****

spring.jpa.hibernate.ddl-auto=none
spring.datasource.username=root
spring.datasource.password=*******
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database=MYSQL
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

【问题讨论】:

  • 配置连接验证和空闲检查。您的连接可能已被终止,并且您不会自动重新连接/清理您的池。
  • 请注意,有一个Spring Boot Cloud SQL starter 可以更轻松地连接到 Cloud SQL。

标签: java mysql spring google-app-engine spring-boot


【解决方案1】:

如果没有更详细的堆栈跟踪很难判断,但您应该替换 Hibernate 的连接池,因为它不适合生产环境。这将处理底层连接。在配置 HikariCP w/ Spring Boot here 上有一个很好的 SO 答案。

此外,您应该查看 Mike 提到的 Spring Boot Cloud SQL starter - 它包含 HikariCP 作为依赖项。

【讨论】:

    猜你喜欢
    • 2021-11-02
    • 2020-02-09
    • 2019-01-23
    • 2021-11-10
    • 1970-01-01
    • 2015-04-20
    • 2016-09-16
    • 1970-01-01
    • 2023-04-08
    相关资源
    最近更新 更多