1 /*
2 * Copyright 2004-2022 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package com.ibatis.sqlmap.client;
17
18 import java.sql.Connection;
19
20 /**
21 * A thread safe client for working with your SQL Maps (Start Here). This interface inherits transaction control and
22 * execution methods from the SqlMapTransactionManager and SqlMapExecutor interfaces.
23 * <p>
24 * The SqlMapClient is the central class for working with SQL Maps. This class will allow you to run mapped statements
25 * (select, insert, update, delete etc.), and also demarcate transactions and work with batches. Once you have an
26 * SqlMapClient instance, everything you need to work with SQL Maps is easily available.
27 * <p>
28 * The SqlMapClient can either be worked with directly as a multi-threaded client (internal session management), or you
29 * can get a single threaded session and work with that. There may be a slight performance increase if you explicitly
30 * get a session (using the openSession() method), as it saves the SqlMapClient from having to manage threads contexts.
31 * But for most cases it won't make much of a difference, so choose whichever paradigm suits your needs or preferences.
32 * <p>
33 * An SqlMapClient instance can be safely made <i>static</i> or applied as a <i>Singleton</i>. Generally it's a good
34 * idea to make a simple configuration class that will configure the instance (using SqlMapClientBuilder) and provide
35 * access to it.
36 * <p>
37 * <b>The following example will demonstrate the use of SqlMapClient.</b>
38 *
39 * <pre>
40 * <i style="color:green">
41 * //
42 * // autocommit simple query --these are just examples...not patterns
43 * //
44 * </i>
45 * Employee emp = (Employee) <b>sqlMap.queryForObject("getEmployee", Integer.valueOf(1))</b>;
46 * <i style="color:green">
47 * //
48 * // transaction --these are just examples...not patterns
49 * //
50 * </i>
51 * try {
52 * <b>sqlMap.startTransaction()</b>
53 * Employee emp2 = new Employee();
54 * // ...set emp2 data
55 * Integer generatedKey = (Integer) <b>sqlMap.insert ("insertEmployee", emp2)</b>;
56 * emp2.setFavouriteColour ("green");
57 * <b>sqlMap.update("updateEmployee", emp2)</b>;
58 * <b>sqlMap.commitTransaction()</b>;
59 * } finally {
60 * <b>sqlMap.endTransaction()</b>;
61 * }
62 * <i style="color:green">
63 * //
64 * // session --these are just examples...not patterns
65 * //
66 * </i>
67 * <code>
68 * try {
69 * <b>SqlMapSession session = sqlMap.openSession()</b>
70 * <b>session.startTransaction()</b>
71 * Employee emp2 = new Employee();
72 * // ...set emp2 data
73 * Integer generatedKey = (Integer) <b>session.insert ("insertEmployee", emp2)</b>;
74 * emp2.setFavouriteColour ("green");
75 * <b>session.update("updateEmployee", emp2)</b>;
76 * <b>session.commitTransaction()</b>;
77 * } finally {
78 * try {
79 * <b>session.endTransaction()</b>;
80 * } finally {
81 * <b>session.close()</b>;
82 * }
83 * // Generally your session scope would be in a wider context and therefore the
84 * // ugly nested finally block above would not be there. Realize that sessions
85 * // MUST be closed if explicitly opened (via openSession()).
86 * }
87 * </code>
88 * <i style="color:green">
89 * //
90 * // batch --these are just examples...not patterns
91 * //
92 * </i>
93 * <code>
94 * try {
95 * <b>sqlMap.startTransaction()</b>
96 * List list = (Employee) <b>sqlMap.queryForList("getFiredEmployees", null)</b>;
97 * <b>sqlMap.startBatch ()</b>;
98 * for (int i=0, n=list.size(); i < n; i++) {
99 * <b>sqlMap.delete ("deleteEmployee", list.get(i))</b>;
100 * }
101 * <b>sqlMap.executeBatch()</b>;
102 * <b>sqlMap.commitTransaction()</b>;
103 * } finally {
104 * <b>sqlMap.endTransaction()</b>;
105 * }
106 * </code>
107 * </pre>
108 *
109 * @see SqlMapClientBuilder
110 * @see SqlMapSession
111 * @see SqlMapExecutor
112 */
113 public interface SqlMapClient extends SqlMapExecutor, SqlMapTransactionManager {
114
115 /**
116 * Returns a single threaded SqlMapSession implementation for use by one user. Remember though, that SqlMapClient
117 * itself is a thread safe SqlMapSession implementation, so you can also just work directly with it. If you do get a
118 * session explicitly using this method <b>be sure to close it!</b> You can close a session using the
119 * sqlMapSession.close() method.
120 * <p>
121 *
122 * @return An SqlMapSession instance.
123 */
124 public SqlMapSession openSession();
125
126 /**
127 * Returns a single threaded SqlMapSession implementation for use by one user. Remember though, that SqlMapClient
128 * itself is a thread safe SqlMapSession implementation, so you can also just work directly with it. If you do get a
129 * session explicitly using this method <b>be sure to close it!</b> You can close a session using the
130 * SqlMapSession.close() method.
131 * <p>
132 * This particular implementation takes a user provided connection as a parameter. This connection will be used for
133 * executing statements, and therefore overrides any configured datasources. Using this approach allows the developer
134 * to easily use an externally supplied connection for executing statements.
135 * <p>
136 * <b>Important:</b> Using a user supplied connection basically sidesteps the datasource so you are responsible for
137 * appropriately handling your connection lifecycle (i.e. closing). Here's a (very) simple example (throws
138 * SQLException):
139 *
140 * <pre>
141 * try {
142 * Connection connection = dataSource.getConnection();
143 * SqlMapSession session = sqlMap.openSession(connection);
144 * // do work
145 * connection.commit();
146 * } catch (SQLException e) {
147 * try {
148 * if (connection != null)
149 * commit.rollback();
150 * } catch (SQLException ignored) {
151 * // generally ignored
152 * }
153 * throw e; // rethrow the exception
154 * } finally {
155 * try {
156 * if (connection != null)
157 * connection.close();
158 * } catch (SQLException ignored) {
159 * // generally ignored
160 * }
161 * }
162 * </pre>
163 *
164 * @param conn
165 * - the connection to use for the session
166 *
167 * @return An SqlMapSession instance.
168 */
169 public SqlMapSession openSession(Connection conn);
170
171 /**
172 * TODO : Deprecated and will be removed.
173 *
174 * @return A session (DEPRECATED)
175 *
176 * @deprecated Use openSession() instead. THIS METHOD WILL BE REMOVED BEFORE FINAL RELEASE.
177 */
178 public SqlMapSession getSession();
179
180 /**
181 * Flushes all data caches.
182 */
183 public void flushDataCache();
184
185 /**
186 * Flushes the data cache that matches the cache model ID provided. cacheId should include the namespace, even when
187 * useStatementNamespaces="false".
188 *
189 * @param cacheId
190 * The cache model to flush
191 */
192 public void flushDataCache(String cacheId);
193
194 /**
195 * Returns a generated implementation of a cusom mapper class as specified by the method parameter. The generated
196 * implementation will run mapped statements by matching the method name to the statement name. The mapped statement
197 * elements determine how the statement is run as per the following:
198 * <ul>
199 * <li><insert> -- insert()
200 * <li><update> -- update()
201 * <li><delete> -- delete()
202 * <li><select> -- queryForObject, queryForList or queryForMap, as determined by signature (see below)
203 * <li><procedure> -- determined by method name (see below)
204 * </ul>
205 * How select statements are run is determined by the method signature, as per the following:
206 * <ul>
207 * <li>Object methodName (Object param) -- queryForObject
208 * <li>List methodName (Object param [, int skip, int max | , int pageSize]) -- queryForList
209 * <li>Map methodName (Object param, String keyProp [,valueProp]) -- queryForMap
210 * </ul>
211 * How stored procedures are run is determined by the method name, as per the following:
212 * <ul>
213 * <li>insertXxxxx -- insert()
214 * <li>createXxxxx -- insert()
215 * <li>updateXxxxx -- update()
216 * <li>saveXxxxx -- update()
217 * <li>deleteXxxxx -- delete()
218 * <li>removeXxxxx -- delete()
219 * <li>selectXxxxx -- queryForXxxxxx() determined by method signature as above
220 * <li>queryXxxxx -- queryForXxxxxx() determined by method signature as above
221 * <li>fetchXxxxx -- queryForXxxxxx() determined by method signature as above
222 * <li>getXxxxx -- queryForXxxxxx() determined by method signature as above
223 * </ul>
224 *
225 * @param iface
226 * The interface that contains methods representing the mapped statements contained.
227 *
228 * @return An instance of iface that can be used to call mapped statements directly in a typesafe manner.
229 */
230 // public Object getMapper(Class iface);
231 }