1 23 24 package org.infoglue.cms.controllers.kernel.impl.simple; 25 26 34 import java.sql.DriverManager ; 35 import java.sql.SQLException ; 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 jdbcMasterConnection = null; 44 java.sql.Connection jdbcSlaveConnection = null; 45 46 String dbRelease = CmsPropertyHandler.getDbRelease(); 47 String dbUser = CmsPropertyHandler.getDbUser(); 48 String dbPassword = CmsPropertyHandler.getDbPassword(); 49 String masterServer = CmsPropertyHandler.getMasterServer(); 50 String 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 getConnection() throws Exception 66 { 67 68 if (masterServer == null) 69 masterServer = "localhost"; 70 71 Class 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 getSlaveConnection() throws Exception 77 { 78 Class 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 executeSQL(String sql,String numOfRows)throws Exception 84 { 85 java.sql.Connection conn = getConnection(); 86 java.sql.Statement stmt = conn.createStatement(); 87 return stmt.executeQuery(sql + " limit " + numOfRows); 88 } 89 90 91 public java.sql.ResultSet executeMasterSQL(String sql) throws Exception 92 { 93 java.sql.Connection conn = getConnection(); 94 java.sql.Statement stmt = conn.createStatement(); 95 return stmt.executeQuery(sql); 96 } 97 public java.sql.ResultSet executeSlaveSQL(String sql) throws Exception 98 { 99 java.sql.Connection conn = getSlaveConnection(); 100 java.sql.Statement stmt = conn.createStatement(); 101 return stmt.executeQuery(sql); 102 } 103 104 public void closeMaster() throws SQLException 105 { 106 if (jdbcMasterConnection != null) 107 jdbcMasterConnection.close(); 108 } 109 110 public void closeSlave() throws SQLException 111 { 112 if (jdbcSlaveConnection != null) 113 jdbcSlaveConnection.close(); 114 } 115 116 } 117 | Popular Tags |