Saturday, 15 January 2011

java ee - How to implement connection-pooling efficiently? -



java ee - How to implement connection-pooling efficiently? -

my code connect database using basicdatasource following, referring ans suppose code creates physical connection database 1 time , provide pool connections afterward. question that, code provide pool connection?, if does, should maintain connection section of code in next method or in constructor provide efficient pooling ? , how should annotations state of class ? @stateful?

// connection section start basicdatasource bs = new basicdatasource(); preparedstatement ps = null; connection con = null; seek { bs.setdriverclassname("com.mysql.jdbc.driver"); bs.seturl("jdbc:mysql://localhost/mydb"); bs.setusername("root"); bs.setpassword(""); con = bs.getconnection(); system.out.println("connecting"); // connection section end ps = con.preparestatement("select *" + " client username = ? "); ps.setstring(1, username); resultset r = ps.executequery(); if (r.next()) { con.close(); homecoming "true"; }

basicdatasource implements connection pooling. however, if create new pool each time it's useless. create info source 1 time , reuse everywhere.

usually, in java ee inject info source. is, container manages info source you. injecting datasource in bean simple as

@resource private datasource moviedatabase;

see this example. configuring datasource depends on container using, though. same info source can used stateful , statless beans.

java-ee jdbc datasource connection-pooling

No comments:

Post a Comment