简体   繁体   中英

java web app refuses to connect to SQL Database when deployed on Tomcat

So i am facing the following problem. I have developed an web app that has the following connection to a SQL Server database. (db connection code attached)

public class DBConnection
{
    private DatabaseMetaData dma;

    private static Connection con;

    private static DBConnection  instance = null;

    private static String  security = "integratedSecurity=true;";

    private DBConnection()
    {

        try {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
        } catch (Exception e) {
            System.out.println("Can not find the driver");
            System.out.println(e.getMessage());
        }

        try{
            con = DriverManager.getConnection("jdbc:jtds:sqlserver://<Machine>;databaseName=<DBName>; useNTLMv2=true;");
            //set autocommit
            con.setAutoCommit(true);
            dma = con.getMetaData(); // get meta data
            System.out.println("Connection to " + dma.getURL());
            System.out.println("Driver " + dma.getDriverName());
            System.out.println("Database product name " + dma.getDatabaseProductName());
        }
        catch(Exception e){

            System.out.println("Problems with the connection to the database");
            System.out.println(e.getMessage());
            System.out.println(con);
        }
    }
    public static void closeConnection()
    {
        try{
            con.close();
            System.out.println("The connection is closed");
        }
        catch (Exception e){
            System.out.println("Error trying to close the database " + e.getMessage());
        }
    }


    public  Connection getDBcon()
    {
        return con;
    }

    public static DBConnection getInstance()
    {
        if (instance == null)
        {
            instance = new DBConnection();
        }
        return instance;
    }

    public static void startTransaction()
    { try{
        con.setAutoCommit(false);
    }
    catch(Exception e){
        System.out.println("fail start transaction");
        System.out.println(e.getMessage());
    }
    }
    public static void commitTransaction()
    { try{
        con.setAutoCommit(true);
    }
    catch(Exception e){
        System.out.println("fail commit transaction");
        System.out.println(e.getMessage());
    }
    }
    public static void rollbackTransaction()
    {
        try
        {
        con.rollback();
        con.setAutoCommit(true);
        }
    catch(Exception e){
        System.out.println("fail rollback transaction");
        System.out.println(e.getMessage());
    }
    }

I am using a Tomcat 8 on InteliJ IDE for running the app and debugging. Which works fine. (DB connection is established)

The problem is that when i take the war file and deploy it in the same Tomcat Server i get no DB Connection. (No DB connection) I have checked all the .jar files in the tomcat and the project and I have added all the needed files. Can't seem to find what is causing this issue. Maybe there is someone who got stuck with the same issue

I can't get a proper undertanding what is causing this issue and how to fix it

**EDIT: Following I have added the error displayed when trying to load data

type Exception report

message Request processing failed; nested exception is java.lang.NullPointerException

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException root cause

java.lang.NullPointerException com.lifeletapp.business.dataLayer.DbLogIn.isValidUser(DbLogIn.java:27) com.lifeletapp.business.HelloController.verifyLogin(HelloController.java:33) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

DbLogin class:

public class DbLogIn implements ILogIn
{
    private Connection conn;

    public DbLogIn()
    {
        conn = DBConnection.getInstance().getDBcon();
    }

    public Staff isValidUser(String userName, String password)
    {
        Staff staff = new Staff();
        try {
            String query = "SELECT * FROM Staff WHERE userName=? AND pass=?";
            PreparedStatement preparedStatement = conn.prepareStatement( query );
            preparedStatement.setString(1, userName);
            preparedStatement.setString(2, password);
            ResultSet resultSet = preparedStatement.executeQuery();
            while( resultSet.next() ) {
                staff.setUserID(resultSet.getInt("userID"));
                staff.setfName(resultSet.getString("fName") );
                staff.setlName(resultSet.getString("lName") );
                staff.setUserName(resultSet.getString("userName") );
                staff.setPass(resultSet.getString("pass") );
                staff.setEmail(resultSet.getString("email") );
            }
            resultSet.close();
            preparedStatement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return staff;
    }
}

This is more a config or server issue. Not a code issue. As mentioned in the comments I have tested if var conn == null -> resulted true. And the connection dose not get nulled anywhere. Please view code. Then again the above code works when run from the InteliJ debugger.

So finally figured out what was the issue. Why was it working on my IDE config: 1. I was using java JDK 1.7 as JAVA_HOME 2. I was using an older version of Tomcat as a build 3. In order to work with JDTS you need to extract the nlmauth.dll from the .jar archive and copy it to the configured env JDK(jdk1.7-->>bin->>copy here)

Why was it not working on Tomcat server: 1.I was using Tomcat 8.xxx 2.Tomcat 8.xx require JDk 8 3. In the JDK 8 i have not past the nlmauth.dll ( once i have done this everything was working)

In order to approach this issue the first clue will be looking into the tomcat server logs.

On my presumption this issue is prom to occur only when you have an Database connection establish via Integrated Security. My presumption is related to the fact that in the tomcat log the main error that was denying the JDBC driver was based on the integrated security credentials.

Best of luck to you all out there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM