KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > db > DbConnectionFactory


1 package com.dotmarketing.db;
2
3 import java.sql.Connection JavaDoc;
4
5 import javax.naming.Context JavaDoc;
6 import javax.naming.InitialContext JavaDoc;
7 import javax.sql.DataSource JavaDoc;
8
9 import com.dotmarketing.exception.DotRuntimeException;
10 import com.dotmarketing.util.Logger;
11
12 public class DbConnectionFactory {
13
14     
15     public static final String JavaDoc MYSQL = "MySQL";
16     public static final String JavaDoc POSTGRESQL = "PostgreSQL";
17     public static final String JavaDoc ORACLE = "Oracle";
18     public static final String JavaDoc MSSQL = "Microsoft SQL Server";
19     
20     private static String JavaDoc _dbType = null;
21
22     private static ThreadLocal JavaDoc connectionHolder = new ThreadLocal JavaDoc();
23
24     public static Connection JavaDoc getConnection() {
25
26         try {
27             Connection JavaDoc connection = (Connection JavaDoc) connectionHolder.get();
28
29             if (connection == null || connection.isClosed()) {
30                 Context JavaDoc ctx = (Context JavaDoc) new InitialContext JavaDoc().lookup("java:comp/env");
31                 DataSource JavaDoc db = (DataSource JavaDoc) ctx.lookup("jdbc/LiferayPool");
32                 connection = db.getConnection();
33                 connectionHolder.set(connection);
34             }
35
36             return connection;
37         } catch (Exception JavaDoc e) {
38             Logger.error(DbConnectionFactory.class, "---------- DBConnectionFactory: error getting dbconnection ---------------", e);
39             throw new DotRuntimeException(e.toString());
40         }
41     }
42
43     public static void closeConnection() {
44         try {
45             Connection JavaDoc connection = (Connection JavaDoc) connectionHolder.get();
46
47             if (connection != null) {
48                 if (!connection.isClosed())
49                     connection.close();
50                 connectionHolder.set(null);
51             }
52         } catch (Exception JavaDoc e) {
53             Logger.error(DbConnectionFactory.class, "---------- DBConnectionFactory: error closing the db dbconnection ---------------", e);
54             throw new DotRuntimeException(e.toString());
55         }
56
57     }
58
59     public static String JavaDoc getDBType() {
60
61         /*
62          * Here is what this out outputs : MySQL PostgreSQL Microsoft SQL Server
63          * Oracle
64          */

65
66         if (_dbType != null) {
67             return _dbType;
68         }
69
70         Connection JavaDoc conn = getConnection();
71
72         try {
73             _dbType = conn.getMetaData().getDatabaseProductName();
74
75         } catch (Exception JavaDoc e) {
76
77         } finally {
78             try {
79                 closeConnection();
80             } catch (Exception JavaDoc e) {
81
82             }
83         }
84
85         return _dbType;
86     }
87
88     public static String JavaDoc getDBTrue() {
89         String JavaDoc x = getDBType();
90
91         if (MYSQL.equals(x)) {
92             return "1";
93         } else if (POSTGRESQL.equals(x)) {
94             return "'true'";
95         } else if (MSSQL.equals(x)) {
96             return "1";
97         } else if (ORACLE.equals(x)) {
98             return "1";
99         }
100         return "true";
101
102     }
103
104     public static String JavaDoc getDBFalse() {
105         String JavaDoc x = getDBType();
106
107         if (MYSQL.equals(x)) {
108             return "0";
109         } else if (POSTGRESQL.equals(x)) {
110             return "'false'";
111         } else if (MSSQL.equals(x)) {
112             return "0";
113         } else if (ORACLE.equals(x)) {
114             return "0";
115         }
116         return "false";
117
118     }
119
120 }
121
Popular Tags