user3272931
user3272931

Reputation: 45

Hibernate datasource connection not closing

I'm having this problem, when @Predestroying my app the datasasource connection is not closing, any idea?

@PreDestroy
public void freeDatasource()  {


    try {
        System.out.println("---------"+ds.getConnection().isClosed());
        ds.getConnection().close();                             
        System.out.println("-------"+ds.getConnection().isClosed());

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
         }

The output is: -----false ----false

Thankyou

Upvotes: 1

Views: 90

Answers (1)

Duncan
Duncan

Reputation: 717

It's because that every time you call ds.getConnection(), your dataSource will return one of the connection instances in the pool. So if you want to test if the connection is off or not, you'd better assign it to a variable then test.

public void freeDatasource()  {

Connection conn = ds.getConnection;
    try {
        System.out.println("---------"+conn.isClosed());
        conn.close();                             
        System.out.println("-------"+conn.isClosed());

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
         }

Upvotes: 2

Related Questions