1 /***
2 * Copyright (c) 2003 held jointly by the individual authors.
3 *
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; with out even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17 *
18 * > http://www.gnu.org/copyleft/lesser.html
19 * > http://www.opensource.org/licenses/lgpl-license.php
20 */
21 package net.mlw.vlh.adapter.jdbc.util;
22
23 import java.sql.Connection;
24 import java.sql.PreparedStatement;
25 import java.sql.ResultSet;
26 import java.sql.SQLException;
27
28 import javax.sql.DataSource;
29
30 /***
31 * The default connection creator, it uses the simplest way to create connection and clear the resources.
32 *
33 * @author Stepan Marek
34 * @version $Revision: 1.3 $ $Date: 2006/03/29 19:47:49 $
35 */
36 public class StandardConnectionCreator implements ConnectionCreator
37 {
38 private DataSource dataSource;
39
40 private int transactionIsolation = 0;
41
42 public StandardConnectionCreator()
43 {}
44
45 public StandardConnectionCreator(DataSource dataSource)
46 {
47 this.dataSource = dataSource;
48 }
49
50 public Connection createConnection() throws SQLException
51 {
52 Connection connection = dataSource.getConnection();
53 switch (transactionIsolation)
54 {
55 case Connection.TRANSACTION_READ_UNCOMMITTED:
56 case Connection.TRANSACTION_READ_COMMITTED:
57 case Connection.TRANSACTION_REPEATABLE_READ:
58 case Connection.TRANSACTION_SERIALIZABLE:
59 connection.setTransactionIsolation(transactionIsolation);
60 connection.setAutoCommit(false);
61 break;
62 }
63 return connection;
64 }
65
66 public void close(ResultSet result, PreparedStatement statement, Connection connection)
67 {
68 JdbcUtil.close(result, statement, connection);
69 }
70
71 public DataSource getDataSource()
72 {
73 return dataSource;
74 }
75
76 public void setDataSource(DataSource dataSource)
77 {
78 this.dataSource = dataSource;
79 }
80 public void setTransactionIsolation(int transactionIsolation)
81 {
82 this.transactionIsolation = transactionIsolation;
83 }
84 }