digitalocean托管数据库-只读异常

时间:2020-08-01 18:58:22

标签: mysql

我正在使用java类来运行我的后端服务,当前我正在使用digitalocean托管数据库,并且遇到了这样一个奇怪的问题。 据我了解,托管数据库具有读写连接性,并且通过我自己的JDBC写连接,我已将写连接设置为readonly(false)和autoCommit(false),但在运行SQL语法时仍会遇到只读异常。

以下是我获得写入连接后的代码

    public synchronized Connection getWriteConnection() throws Exception {
        Connection conn = null;
        if (!busy) {
            if (connectionPool.isEmpty()) {
                Class.forName(ServerProperties.DB_WRITE_DRIVER);
                conn = (Connection) DriverManager.getConnection(ServerProperties.DB_WRITE_URL, ServerProperties.DB_WRITE_USERNAME, ServerProperties.DB_WRITE_PASSWORD);
                conn.setReadOnly(false);
                conn.setAutoCommit(false);
                
                if (conn.isReadOnly()) {
                    conn.setReadOnly(false);
                }
            } else {
                conn = connectionPool.get(0);
                conn.setReadOnly(false);
                conn.setAutoCommit(false);
                
                connectionPool.remove(connectionPool.get(0));
                connectionPool.trimToSize();
            }
        } else {
            return getWriteConnection();
        }

        return conn;
    }
SQL State     : HY000
SQL Exception : The MySQL server is running with the --read-only option so it cannot execute this statement
Error Code    : 1290
com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953)
com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1092)
com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1040)
com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1347)
com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:1025)

0 个答案:

没有答案
相关问题