KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nemesis > forum > util > jdbc > DbConnectionManager


1 /*
2  * NEMESIS-FORUM.
3  * Copyright (C) 2002 David Laurent(lithium2@free.fr). All rights reserved.
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
6  *
7  * Copyright (C) 2001 Yasna.com. All rights reserved.
8  *
9  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
10  *
11  * NEMESIS-FORUM. is free software; you can redistribute it and/or
12  * modify it under the terms of the Apache Software License, Version 1.1,
13  * or (at your option) any later version.
14  *
15  * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
16  * application are parts of NEMESIS-FORUM and are distributed under
17  * same terms of licence.
18  *
19  *
20  * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
21  * and software developed by CoolServlets.com (http://www.coolservlets.com).
22  * and software developed by Yasna.com (http://www.yasna.com).
23  *
24  */

25
26
27 package org.nemesis.forum.util.jdbc;
28
29 import java.sql.Connection JavaDoc;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.nemesis.forum.config.ConfigLoader;
34
35
36
37 /**
38  * Central manager of database connections. All methods are static so that they
39  * can be easily accessed throughout the classes in the database package.
40  */

41 public class DbConnectionManager {
42
43     static protected Log log =LogFactory.getLog(DbConnectionManager.class);
44
45     private static DbConnectionProvider connectionProvider;
46     private static Object JavaDoc providerLock = new Object JavaDoc();
47
48     /**
49      * Returns a database connection from the currently active connection
50      * provider.
51      */

52     public static Connection JavaDoc getConnection() {
53         if (connectionProvider == null) {
54             synchronized (providerLock) {
55                 if (connectionProvider == null) {
56                     //Attempt to load the class.
57
try {
58                         
59                         connectionProvider =(DbConnectionProvider) Class.forName(ConfigLoader.getInstance().getConfig().getJDBCConnectionProviderClass()).newInstance();
60                         connectionProvider.start();
61                     } catch (Exception JavaDoc e) {
62                         log.fatal(
63                             "check db.connectionProvider.className in nemesis-forum-config.properties:"+ConfigLoader.getInstance().getConfig().getJDBCConnectionProviderClass(),
64                             e);
65                     }
66                 }
67
68             }
69
70         }
71         return connectionProvider.getConnection();
72     }
73
74     /**
75      * Returns the current connection provider. The only case in which this
76      * method should be called is if more information about the current
77      * connection provider is needed. Database connections should always be
78      * obtained by calling the getConnection method of this class.
79      */

80     public static DbConnectionProvider getDbConnectionProvider() {
81         return connectionProvider;
82     }
83
84     /**
85      * Sets the connection provider. The old provider (if it exists) is shut
86      * down before the new one is started. A connection provider <b>should
87      * not</b> be started before being passed to the connection manager
88      * because the manager will call the start() method automatically.
89      */

90     public static void setDbConnectionProvider(DbConnectionProvider provider) {
91         synchronized (providerLock) {
92             if (connectionProvider != null) {
93                 connectionProvider.destroy();
94                 connectionProvider = null;
95             }
96             connectionProvider = provider;
97             provider.start();
98         }
99     }
100
101 }
102
Popular Tags