1 //
2 // Copyright (c) 2003-2005, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.
14 // nor the names of its contributors may be used to endorse or promote products
15 // derived from this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 // POSSIBILITY OF SUCH DAMAGE.
27 //
28
29 package org.objectledge.database;
30
31 import java.sql.PreparedStatement;
32 import java.sql.ResultSet;
33 import java.sql.SQLException;
34
35 import org.objectledge.database.ThreadDataSource.ThreadConnection;
36 import org.objectledge.database.impl.DelegatingPreparedStatement;
37
38 /***
39 * PreparedStatement wrapper that monitors the number of DB reads/writes, and their duration.
40 *
41 * @author <a href="rafal@caltha.pl">RafaĆ Krzewski</a>
42 * @version $Id: MonitoringPreparedStatement.java,v 1.1 2005/10/10 09:44:50 rafal Exp $
43 */
44 public class MonitoringPreparedStatement
45 extends DelegatingPreparedStatement
46 {
47 private final ThreadConnection threadConn;
48
49 /***
50 * Creates a new MonitoringPreparedStatement instance.
51 *
52 * @param preparedStatement delegate prepared statement.
53 * @param sql statement body.
54 * @param threadConn the associated ThreadConnection.
55 */
56 public MonitoringPreparedStatement(PreparedStatement preparedStatement, String sql,
57 ThreadConnection threadConn)
58 {
59 super(preparedStatement, sql);
60 this.threadConn = threadConn;
61 }
62
63 /***
64 * {@inheritDoc}
65 */
66 @Override
67 public boolean execute()
68 throws SQLException
69 {
70 threadConn.startStatement(getBody());
71 try
72 {
73 return super.execute();
74 }
75 finally
76 {
77 threadConn.finishStatement(getBody());
78 }
79 }
80
81 /***
82 * {@inheritDoc}
83 */
84 @Override
85 public ResultSet executeQuery()
86 throws SQLException
87 {
88 threadConn.startStatement(getBody());
89 try
90 {
91 return super.executeQuery();
92 }
93 finally
94 {
95 threadConn.finishStatement(getBody());
96 }
97 }
98
99 /***
100 * {@inheritDoc}
101 */
102 @Override
103 public int executeUpdate()
104 throws SQLException
105 {
106 threadConn.startStatement(getBody());
107 try
108 {
109 return super.executeUpdate();
110 }
111 finally
112 {
113 threadConn.finishStatement(getBody());
114 }
115 }
116
117 /***
118 * {@inheritDoc}
119 */
120 @Override
121 public int[] executeBatch()
122 throws SQLException
123 {
124 threadConn.startStatement(getBatchBuffer());
125 try
126 {
127 return super.executeBatch();
128 }
129 finally
130 {
131 threadConn.finishStatement(getBatchBuffer());
132 }
133 }
134 }