i think i don't get the utility of a connection pooling, since i have this
situation: 30 threads try to perform at same time a db access with the call:
new Database().doSomething()
My Database class is done like that:
public class Database {
private long start = 0;
private Connection getConnection() throws Exception {
// get start timestamp
start = System.currentTimeMillis();
// get context: provides the starting point for resolution of
names
Context ctx = new InitialContext();
if (ctx == null) {
throw new Exception("No Context");
}
// retrieve datasource
DataSource ds = (DataSource)
ctx.lookup("java:comp/env/jdbc/someDB");
if (ds == null) {
throw new Exception("No Datasource");
}
// return db connection
return ds.getConnection();
}
// close connection resources
private void closeResources(Connection con, Statement stmt, ResultSet
rst, String method) {
try {
if (rst != null) rst.close();
rst = null;
}
catch (Exception e1) {
// log things
}
try {
if (stmt != null) stmt.close();
stmt = null;
}
catch (Exception e2) {
// log things
}
try {
if (con != null) con.close();
con = null;
}
catch (Exception e3) {
// log things
}
// log elapsed time
System.out.println("Database -> " + method + ": " +
(System.currentTimeMillis() - start));
}
// do something
public void doSomething() {
Connection con = null;
Statement stmt = null;
ResultSet rst = null;
try {
// get connection
con = getConnection();
if (con == null) {
throw new Exception("No Connection");
}
stmt = con.createStatement();
// perform query
rst = stmt
.executeQuery("SELECT * FROM table1");
if (rst.next()) {
// do stuff
}
else {
throw new Exception("No results");
}
}
catch (Exception e1) {
// log things
}
// close resources
finally {
closeResources(con, stmt, rst, "doSomething");
}
}
}
When the 30 threads try to do call doSomething() method from Database object, i
got this log:
[wed, 09 apr 2008 00:55:03] INFO Database -> doSomething: 836
[wed, 09 apr 2008 00:55:04] INFO Database -> doSomething: 1444
[wed, 09 apr 2008 00:55:04] INFO Database -> doSomething: 2054
[wed, 09 apr 2008 00:55:05] INFO Database -> doSomething: 2417
[wed, 09 apr 2008 00:55:05] INFO Database -> doSomething: 3060
[wed, 09 apr 2008 00:55:06] INFO Database -> doSomething: 3647
[wed, 09 apr 2008 00:55:07] INFO Database -> doSomething: 4279
[wed, 09 apr 2008 00:55:07] INFO Database -> doSomething: 4967
[wed, 09 apr 2008 00:55:08] INFO Database -> doSomething: 5592
[wed, 09 apr 2008 00:55:09] INFO Database -> doSomething: 6208
[wed, 09 apr 2008 00:55:09] INFO Database -> doSomething: 7026
[wed, 09 apr 2008 00:55:10] INFO Database -> doSomething: 7455
[wed, 09 apr 2008 00:55:10] INFO Database -> doSomething: 8087
[wed, 09 apr 2008 00:55:11] INFO Database -> doSomething: 8705
[wed, 09 apr 2008 00:55:12] INFO Database -> doSomething: 9318
[wed, 09 apr 2008 00:55:12] INFO Database -> doSomething: 10040
[wed, 09 apr 2008 00:55:13] INFO Database -> doSomething: 11146
[wed, 09 apr 2008 00:55:14] INFO Database -> doSomething: 11702
[wed, 09 apr 2008 00:55:14] INFO Database -> doSomething: 12033
[wed, 09 apr 2008 00:55:15] INFO Database -> doSomething: 12658
[wed, 09 apr 2008 00:55:16] INFO Database -> doSomething: 13279
[wed, 09 apr 2008 00:55:16] INFO Database -> doSomething: 13897
[wed, 09 apr 2008 00:55:17] INFO Database -> doSomething: 14523
[wed, 09 apr 2008 00:55:17] INFO Database -> doSomething: 15139
[wed, 09 apr 2008 00:55:18] INFO Database -> doSomething: 15759
[wed, 09 apr 2008 00:55:19] INFO Database -> doSomething: 16411
[wed, 09 apr 2008 00:55:19] INFO Database -> doSomething: 17056
[wed, 09 apr 2008 00:55:20] INFO Database -> doSomething: 17672
[wed, 09 apr 2008 00:55:20] INFO Database -> doSomething: 18292
Last thread perform the operation in 18 seconds? that means it create database
object when the other threads do, then wait for a connection? I thought
connection pooling allow more than one connection, it seems that only one is
active in this case, isn't it?
My context.xml file is:
<Context debug="5" reloadable="true" crossContext="true">
<Resource
name="jdbc/someDB"
auth="Container"
type="javax.sql.DataSource"
maxActive="600"
maxIdle="300"
maxWait="15000"
username="someUser"
password="somePass"
driverClassName="com.mysql.jdbc.Driver"
validationQuery="SELECT 1"
url="jdbc:mysql://1.1.1.1:3306/somePath"/>
</Context>
so i can't understand the meaning of maxActive parameter.. i thought that could
be at most 600 concurrent connection.I'm doing something wrong or connection
pooling is not what i had in mind?
Thanks
---------------------------------------------------------------------
To start a new topic, e-mail: [email protected]
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]