DriverManager仅在Tomcat上保持返回null连接(在Glassfish上正常工作)

时间:2017-09-19 15:32:41

标签: mysql tomcat null glassfish centos7

我在几天前将Tomcat 7升级到7.0.26(Centos 7 yum update)之后遇到了一个奇怪的情况。当在Tomcat服务器上部署servlet时,DriverManager无法自动识别MySQL驱动程序并返回null db Connection。另一方面,我的NetBeans上的GlassFish服务器工作正常。

简单的测试servlet代码如下所示:

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter())
    {
        Connection c = MySQLConnector.connect("DBFoobar", "username", "passwd");
        String message = null;
        if (c == null)
            message = "Connection returned null";
        else
            message = "C is returned: " + c.toString();
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<body>");
        out.println(message);
        .
        .
        .

并在MySQLConnector中,jdbc调用定义如下:

public class MySQLConnector
{
    private static final String dbURLPrefix
            = "jdbc:mysql://dbfoobar.xxx.uswest2.rds.amazonaws.com:3306/";
    public static Connection connect(String dbName, String username, String password)
    {
        Connection connection = null;
        try
        {
           Class.forName("com.mysql.jdbc.Driver");
           connection = DriverManager.getConnection(dbURLPrefix + dbName 
                + "?useUnicode=yes&characterEncoding=UTF-8&useSSL=false", username, password);
           .
           .
           .

当在GlasshFish服务器上运行servlet时,它正确显示Connection对象,但在Tomcat服务器上显示Connection returned null消息。

有关于此的任何想法吗?到目前为止,我已经尝试将mysql连接器jar文件放在/usr/share/tomcat/lib目录下,但无济于事。 war包正确包含mysql连接器jar文件。以前版本的Tomcat 7运行良好。

2 个答案:

答案 0 :(得分:0)

终于找到了答案。这是由于SELinux。我没有检查细节,但SELinux似乎阻止DriveManager执行。我修改了配置文件/etc/selinux/config并禁用了SELinux:

SELINUX=permissive    # changed from enforcing

答案 1 :(得分:-1)

好。我放弃了。 :)我能找到的唯一的事情如下。我使用的是CentOS 7。

  • 从Tomcat 7.0.69升级到7.0.76似乎已经打破Connection DriverManager/var/log/yum.log
  • 7.0.69开始,OpenJDK(1.8.0)也已升级
  • 将整个虚拟EC2实例恢复为先前的备份(使用Tomcat yum update)后,代码运行没有任何问题
  • <!DOCTYPE html> <html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> <title>Reverse Geocoding</title> <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> <script type="text/javascript"> var geocoder; if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(successFunction, errorFunction); } // Get the latitude and the longitude; function successFunction(position) { var lat = position.coords.latitude; var lng = position.coords.longitude; codeLatLng(lat, lng); } function errorFunction() { alert("Geocoder failed"); } function codeLatLng(lat, lng) { geocoder = new google.maps.Geocoder(); var latlng = new google.maps.LatLng(lat, lng); geocoder.geocode({ latLng: latlng }, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { if (results[1]) { var arrAddress = results; console.log(results); $.each(arrAddress, function(i, address_component) { if (address_component.types[0] == "locality") { console.log("City: " + address_component.address_components[0].long_name); itemLocality = address_component.address_components[0].long_name; } }); } else { alert("No results found"); } } else { alert("Geocoder failed due to: " + status); } }); } </script> </head> <body> <a href="" onClick="initialize();">Get position</a> </body> </html> 再次导致上述错误

希望这会有所帮助。