简体   繁体   中英

Spring JDBC error connection

I use Tomcat with Spring (with web services Jersey async) and i set a pool connection in Tomcat like this:

<Resource auth="Container" driverClassName="com.mysql.jdbc.Driver"
    maxActive="100" maxIdle="50" maxWait="10000" name="jdbc/UsersDB"
    password="secret" type="javax.sql.DataSource"  url="jdbc:mysql://localhost:3306/DB"
    username="root" />

In my JDBC class for example i have:

    public String getMFromToken(String token) {

    logger.info(TAG_LOG + " "
            + "prima della richiesta JDBC id" );

    int id = jdbcTemplateObject.queryForObject(sqlGetToken, new Object [] {token}, Integer.class);

    logger.info(TAG_LOG + " "
            + "recuperato id " + id );

    String sqlGetMFromCred = "select matricola from stud where cred_id = ?";

    logger.info(TAG_LOG + " "
            + "prima della richiesta JDBC matricola" );

    String matricola = jdbcTemplateObject.queryForObject(sqlGetMFromCred, new Object [] {id},String.class);

    logger.info(TAG_LOG + " "
            + "recuperata matricola " + matricola );

    return matricola;
}

If I do not do requests (requests that require a query to the database) to tomcat for a long time, tomcat response me a 500 error, but if i call any requests (requests not require a query to the db) all works.

So the problem is some configuration of Spring (i think)..Why Spring not work if i wait long time to do any request ?

sorry for my bad english

While it would be helpful to see the error message, it sounds like your MySQL server is disconnecting your pooled connections.

You said that you only see this problem when you let your app sit for a while without sending any requests to the database. As your app sits without using the database connections, your MySQL server will see the connections haven't been used in some period of time (this is configurable if you control the server) and disconnect them.

Because the server disconnects them, the connection pool doesn't yet realize that they have been closed. This creates a problem because the next time you go to use a connection, the pool gives your app a bad connection. While I can't be certain because I haven't seen the exact error message, this could certainly cause you to get a 500 error.

What I'd suggest is that you add the testOnBorrow and validationQuery attributes to your connection pool configuration (you should always have these set anyway). This will instruct the pool to test connections with the query you specify before it gives them to your application. In other words, the pool will filter out any connections which are bad and replace them with new, good connections.

<Resource auth="Container" driverClassName="com.mysql.jdbc.Driver"
    maxActive="100" maxIdle="50" maxWait="10000" name="jdbc/UsersDB"
    password="secret" type="javax.sql.DataSource"  url="jdbc:mysql://localhost:3306/DB"
    username="root" testOnBorrow="true" validationQuery="SELECT 1" />

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