access denied (“java.lang.RuntimePermission” “modi

2020-03-30 07:38发布

I'm developing a server application using Google App Engine and Google Cloud SQL. I have a problem when trying to connect to the Cloud SQL when running the server on my local machine. I get the error:

java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "modifyThreadGroup")

Here is the servlet code:

package com.example.example;

import com.google.appengine.api.utils.SystemProperty;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.servlet.http.*;

public class MyServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/plain");
        resp.getWriter().println("Please use the form to POST to this url");
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String url = null;
        try {
            if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production) {
                // Load the class that provides the new "jdbc:google:mysql://" prefix.
                Class.forName("com.mysql.jdbc.GoogleDriver");
                url = "jdbc:google:mysql://app_id:instance_id/data?user=root";
            } else {
                // Local MySQL instance to use during development.
                Class.forName("com.mysql.jdbc.Driver");
                url = "jdbc:mysql://instance-ip:3306/data?user=root";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        PrintWriter out = resp.getWriter();
        try {
            Connection conn = DriverManager.getConnection(url);
            System.out.println("Connection established");
            out.println("Connection established");
            try {
            } finally {
                conn.close();
            }
        } catch (SQLException e) {
            out.append(e.getMessage());
            e.printStackTrace();
        }
    }

}

I have added the following line to the java.policy file:

permission java.lang.RuntimePermission "modifyThreadGroup";

But this did not fix the problem.

0条回答
登录 后发表回答