| 1 | /* WRS 2.0 |
|---|
| 2 | * Copyright (C) 2009 Anton Patrushev |
|---|
| 3 | * |
|---|
| 4 | * This program is free software; you can redistribute it and/or modify |
|---|
| 5 | * it under the terms of the GNU General Public License as published by |
|---|
| 6 | * the Free Software Foundation; either version 3 of the License, or |
|---|
| 7 | * (at your option) any later version. |
|---|
| 8 | * |
|---|
| 9 | * This program is distributed in the hope that it will be useful, |
|---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 | * GNU General Public License for more details. |
|---|
| 13 | * |
|---|
| 14 | * You should have received a copy of the GNU General Public License |
|---|
| 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 16 | */ |
|---|
| 17 | |
|---|
| 18 | package util.db; |
|---|
| 19 | |
|---|
| 20 | import java.sql.Connection; |
|---|
| 21 | import java.sql.DriverManager; |
|---|
| 22 | import java.sql.SQLException; |
|---|
| 23 | |
|---|
| 24 | import util.ObjectPool; |
|---|
| 25 | |
|---|
| 26 | public class JDBCConnectionPool extends ObjectPool<Connection> |
|---|
| 27 | { |
|---|
| 28 | |
|---|
| 29 | public JDBCConnectionPool(String url, String user, |
|---|
| 30 | String password) |
|---|
| 31 | { |
|---|
| 32 | super(url, user, password); |
|---|
| 33 | |
|---|
| 34 | try |
|---|
| 35 | { |
|---|
| 36 | Class.forName("org.postgresql.Driver").newInstance(); |
|---|
| 37 | } |
|---|
| 38 | catch (Exception e) |
|---|
| 39 | { |
|---|
| 40 | e.printStackTrace(); |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | @Override |
|---|
| 46 | protected Connection create() |
|---|
| 47 | { |
|---|
| 48 | try |
|---|
| 49 | { |
|---|
| 50 | return (DriverManager.getConnection(url, user, password)); |
|---|
| 51 | } |
|---|
| 52 | catch (SQLException e) |
|---|
| 53 | { |
|---|
| 54 | e.printStackTrace(); |
|---|
| 55 | return (null); |
|---|
| 56 | } |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | @Override |
|---|
| 60 | public void expire(Connection o) |
|---|
| 61 | { |
|---|
| 62 | try |
|---|
| 63 | { |
|---|
| 64 | ((Connection) o).close(); |
|---|
| 65 | } |
|---|
| 66 | catch (SQLException e) |
|---|
| 67 | { |
|---|
| 68 | e.printStackTrace(); |
|---|
| 69 | } |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | @Override |
|---|
| 73 | public boolean validate(Connection o) |
|---|
| 74 | { |
|---|
| 75 | try |
|---|
| 76 | { |
|---|
| 77 | return (!((Connection) o).isClosed()); |
|---|
| 78 | } |
|---|
| 79 | catch (SQLException e) |
|---|
| 80 | { |
|---|
| 81 | e.printStackTrace(); |
|---|
| 82 | return (false); |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | } |
|---|