2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
17
package org.apache.tomcat.jdbc.test;
19
import java.sql.Connection;
20
import java.sql.PreparedStatement;
22
import org.apache.tomcat.jdbc.pool.interceptor.StatementCache;
24
public class TestStatementCache extends DefaultTestCase {
27
public TestStatementCache(String name) {
31
private static volatile TestStatementCacheInterceptor interceptor = null;
35
protected void tearDown() throws Exception {
36
// TODO Auto-generated method stub
37
this.interceptor = null;
42
private void config(boolean cachePrepared, boolean cacheCallable, int max) {
43
datasource.getPoolProperties().setJdbcInterceptors(TestStatementCacheInterceptor.class.getName()+
44
"(prepared="+cachePrepared+",callable="+cacheCallable+",max="+max+")");
47
public void testIsCacheEnabled() throws Exception {
50
datasource.getConnection().close();
51
assertNotNull("Interceptor was not created.", interceptor);
54
public void testCacheProperties() throws Exception {
57
datasource.getConnection().close();
58
assertEquals(true, interceptor.isCacheCallable());
59
assertEquals(true, interceptor.isCachePrepared());
60
assertEquals(50,interceptor.getMaxCacheSize());
63
public void testCacheProperties2() throws Exception {
65
config(false,false,100);
66
datasource.getConnection().close();
67
assertEquals(false, interceptor.isCacheCallable());
68
assertEquals(false, interceptor.isCachePrepared());
69
assertEquals(100,interceptor.getMaxCacheSize());
72
public void testPreparedStatementCache() throws Exception {
74
config(true,false,100);
75
Connection con = datasource.getConnection();
76
PreparedStatement ps1 = con.prepareStatement("select 1");
77
PreparedStatement ps2 = con.prepareStatement("select 1");
78
assertEquals(0,interceptor.getCacheSize().get());
80
assertTrue(ps1.isClosed());
81
assertEquals(1,interceptor.getCacheSize().get());
82
PreparedStatement ps3 = con.prepareStatement("select 1");
83
assertEquals(0,interceptor.getCacheSize().get());
85
assertTrue(ps2.isClosed());
87
assertTrue(ps3.isClosed());
88
assertEquals(1,interceptor.getCacheSize().get());
91
public void testPreparedStatementCache2() throws Exception {
93
config(false,false,100);
94
Connection con = datasource.getConnection();
95
PreparedStatement ps1 = con.prepareStatement("select 1");
96
PreparedStatement ps2 = con.prepareStatement("select 1");
97
assertEquals(0,interceptor.getCacheSize().get());
99
assertTrue(ps1.isClosed());
100
assertEquals(0,interceptor.getCacheSize().get());
101
PreparedStatement ps3 = con.prepareStatement("select 1");
102
assertEquals(0,interceptor.getCacheSize().get());
104
assertTrue(ps2.isClosed());
106
assertTrue(ps3.isClosed());
107
assertEquals(0,interceptor.getCacheSize().get());
110
public void testCallableStatementCache() throws Exception {
113
public void testMaxCacheSize() throws Exception {
115
config(true,false,100);
116
Connection con1 = datasource.getConnection();
117
Connection con2 = datasource.getConnection();
118
for (int i=0; i<120; i++) {
119
Connection con = (i%2==0)?con1:con2;
120
PreparedStatement ps = con.prepareStatement("select "+i);
123
assertEquals(100,interceptor.getCacheSize().get());
127
public static class TestStatementCacheInterceptor extends StatementCache {
128
public TestStatementCacheInterceptor() {
129
TestStatementCache.interceptor = this;