KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > controllers > kernel > impl > simple > MysqlJDBCService


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.controllers.kernel.impl.simple;
25
26 /**
27  * @author mgu
28  *
29  * SS 021030 - Added support for getting jdbc connection for this builds dbrelease
30  * and getting connection for corresponding dbrelease on slave server.
31  *
32  * this one needs to be rewritten.
33  */

34 import java.sql.DriverManager JavaDoc;
35 import java.sql.SQLException JavaDoc;
36
37 import org.infoglue.cms.util.CmsPropertyHandler;
38
39 import com.opensymphony.util.TextUtils;
40
41 public class MysqlJDBCService
42 {
43     java.sql.Connection JavaDoc jdbcMasterConnection = null;
44     java.sql.Connection JavaDoc jdbcSlaveConnection = null;
45
46     String JavaDoc dbRelease = CmsPropertyHandler.getDbRelease();
47     String JavaDoc dbUser = CmsPropertyHandler.getDbUser();
48     String JavaDoc dbPassword = CmsPropertyHandler.getDbPassword();
49     String JavaDoc masterServer = CmsPropertyHandler.getMasterServer();
50     String JavaDoc slaveServer = CmsPropertyHandler.getSlaveServer();
51
52     private boolean enabled = false;
53
54     public MysqlJDBCService()
55     {
56         if(TextUtils.stringSet(masterServer) && TextUtils.stringSet(slaveServer))
57             enabled = true;
58     }
59
60     public boolean isEnabled()
61     {
62         return enabled;
63     }
64
65     private java.sql.Connection JavaDoc getConnection() throws Exception JavaDoc
66     {
67
68         if (masterServer == null)
69             masterServer = "localhost";
70
71         Class JavaDoc driverClass = Class.forName("com.mysql.jdbc.Driver");
72         this.jdbcMasterConnection = DriverManager.getConnection("jdbc:mysql://" + masterServer + "/" + dbRelease, dbUser, dbPassword);
73         return jdbcMasterConnection;
74     }
75
76     private java.sql.Connection JavaDoc getSlaveConnection() throws Exception JavaDoc
77     {
78         Class JavaDoc driverClass = Class.forName("com.mysql.jdbc.Driver");
79         this.jdbcSlaveConnection = DriverManager.getConnection("jdbc:mysql://" + slaveServer + "/" + dbRelease, dbUser, dbPassword);
80         return jdbcSlaveConnection;
81     }
82
83     public java.sql.ResultSet JavaDoc executeSQL(String JavaDoc sql,String JavaDoc numOfRows)throws Exception JavaDoc
84     {
85         java.sql.Connection JavaDoc conn = getConnection();
86         java.sql.Statement JavaDoc stmt = conn.createStatement();
87         return stmt.executeQuery(sql + " limit " + numOfRows);
88     }
89
90
91     public java.sql.ResultSet JavaDoc executeMasterSQL(String JavaDoc sql) throws Exception JavaDoc
92     {
93         java.sql.Connection JavaDoc conn = getConnection();
94         java.sql.Statement JavaDoc stmt = conn.createStatement();
95         return stmt.executeQuery(sql);
96     }
97     public java.sql.ResultSet JavaDoc executeSlaveSQL(String JavaDoc sql) throws Exception JavaDoc
98     {
99         java.sql.Connection JavaDoc conn = getSlaveConnection();
100         java.sql.Statement JavaDoc stmt = conn.createStatement();
101         return stmt.executeQuery(sql);
102     }
103
104     public void closeMaster() throws SQLException JavaDoc
105     {
106         if (jdbcMasterConnection != null)
107             jdbcMasterConnection.close();
108     }
109
110     public void closeSlave() throws SQLException JavaDoc
111     {
112         if (jdbcSlaveConnection != null)
113             jdbcSlaveConnection.close();
114     }
115
116 }
117
Popular Tags