mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
git-svn-id: http://code.alibabatech.com/svn/druid/trunk@8 b9813039-fb51-4c41-a8b9-e21c2acb5095
146 lines
3.5 KiB
Java
146 lines
3.5 KiB
Java
/*
|
|
* Copyright 2011 Alibaba Group.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
package com.alibaba.druid.bvt.proxy;
|
|
|
|
import java.math.BigDecimal;
|
|
import java.sql.Connection;
|
|
import java.sql.DriverManager;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import java.sql.Statement;
|
|
import java.util.concurrent.atomic.AtomicLong;
|
|
|
|
import javax.management.openmbean.TabularData;
|
|
|
|
import junit.framework.Assert;
|
|
import junit.framework.TestCase;
|
|
|
|
import com.alibaba.druid.stat.JdbcStatManager;
|
|
import com.alibaba.druid.util.JdbcUtils;
|
|
|
|
public class AllStatisticTest extends TestCase {
|
|
String url = "jdbc:wrap-jdbc:filters=default,commonLogging,log4j:name=statTest:jdbc:derby:classpath:petstore-db";
|
|
|
|
private AtomicLong fetchRowCout = new AtomicLong();
|
|
|
|
Connection globalConnection = null;
|
|
|
|
protected void setUp() throws Exception {
|
|
JdbcStatManager stat = JdbcStatManager.getInstance();
|
|
|
|
stat.reset();
|
|
|
|
Class.forName("com.alibaba.druid.proxy.DruidDriver");
|
|
|
|
Connection conn = DriverManager.getConnection(url);
|
|
|
|
int size = stat.getConnectionList().size();
|
|
Assert.assertTrue(size >= 1);
|
|
conn.close();
|
|
|
|
TabularData connectionList = stat.getConnectionList();
|
|
|
|
Assert.assertEquals(connectionList.size(), size - 1);
|
|
|
|
stat.reset();
|
|
|
|
globalConnection = DriverManager.getConnection(url);
|
|
}
|
|
|
|
protected void tearDown() throws Exception {
|
|
JdbcUtils.close(globalConnection);
|
|
}
|
|
|
|
public void test_stmt() throws Exception {
|
|
|
|
// ////////////////////////
|
|
|
|
|
|
f1();
|
|
f2();
|
|
f3();
|
|
|
|
|
|
|
|
}
|
|
|
|
public void f1() throws Exception {
|
|
|
|
Statement pstmt = null;
|
|
ResultSet rs = null;
|
|
|
|
try {
|
|
|
|
pstmt = globalConnection.createStatement();
|
|
rs = pstmt.executeQuery("SELECT * FROM ITEM WHERE LISTPRICE > 10");
|
|
while (rs.next()) {
|
|
fetchRowCout.incrementAndGet();
|
|
rs.getObject(1);
|
|
}
|
|
} finally {
|
|
JdbcUtils.close(rs);
|
|
JdbcUtils.close(pstmt);
|
|
}
|
|
}
|
|
|
|
public void f2() throws Exception {
|
|
|
|
Connection conn = null;
|
|
Statement pstmt = null;
|
|
ResultSet rs = null;
|
|
|
|
try {
|
|
conn = DriverManager.getConnection(url);
|
|
|
|
pstmt = conn.createStatement();
|
|
rs = pstmt.executeQuery("SELECT * FROM ITxEM WHERE LISTPRICE > 10");
|
|
while (rs.next()) {
|
|
fetchRowCout.incrementAndGet();
|
|
rs.getObject(1);
|
|
}
|
|
} catch (SQLException ex) {
|
|
} finally {
|
|
JdbcUtils.close(rs);
|
|
JdbcUtils.close(pstmt);
|
|
JdbcUtils.close(conn);
|
|
}
|
|
}
|
|
|
|
public void f3() throws Exception {
|
|
|
|
Connection conn = null;
|
|
PreparedStatement pstmt = null;
|
|
ResultSet rs = null;
|
|
|
|
try {
|
|
conn = DriverManager.getConnection(url);
|
|
|
|
pstmt = conn.prepareStatement("SELECT * FROM ITEM WHERE LISTPRICE > ?");
|
|
pstmt.setBigDecimal(1, new BigDecimal(10));
|
|
rs = pstmt.executeQuery();
|
|
while (rs.next()) {
|
|
fetchRowCout.incrementAndGet();
|
|
rs.getObject(1);
|
|
}
|
|
} finally {
|
|
JdbcUtils.close(rs);
|
|
JdbcUtils.close(pstmt);
|
|
JdbcUtils.close(conn);
|
|
}
|
|
}
|
|
}
|