@@ -34,6 +34,7 @@ class DbMysql extends Db{
3434 * @param array $config 数据库配置数组
3535 +----------------------------------------------------------
3636 */
37+ private $ is_spare =false ;//[sae]是否连接的为备用数据库
3738 public function __construct ($ config ='' ){
3839 if ( !extension_loaded ('mysql ' ) ) {
3940 throw_exception (L ('_NOT_SUPPERT_ ' ).':mysql ' );
@@ -69,9 +70,26 @@ public function connect($config='',$linkNum=0,$force=false) {
6970 }
7071 if ( !$ this ->linkID [$ linkNum ] || (!empty ($ config ['database ' ]) && !mysql_select_db ($ config ['database ' ], $ this ->linkID [$ linkNum ])) ) {
7172 $ errStr =mysql_error ();
72- //[sae] 短信预警
73- if (C ('SMS_ON ' )) Sms::send ('数据库连接时出错,请在SAE日志中心查看详情 ' , $ errStr ,Sms::MYSQL_ERROR );
74- throw_exception ($ errStr );
73+ $ errno =mysql_errno ();
74+ if ($ errno ==13047 ){
75+ if (C ('SMS_ON ' )) Sms::send ('mysql超额被禁用,请在SAE日志中心查看详情 ' , $ errStr ,Sms::MYSQL_ERROR );
76+ //[sae]启动备用数据库
77+ if (C ('SPARE_DB_HOST ' )){
78+ $ this ->linkID [$ linkNum ]=mysql_connect ( C ('SPARE_DB_HOST ' ).(C ('SPARE_DB_PORT ' )?': ' .C ('SPARE_DB_PORT ' ):'' ), C ('SPARE_DB_USER ' ), C ('SPARE_DB_PWD ' ),true ,CLIENT_MULTI_RESULTS );
79+ mysql_select_db (C ('SPARE_DB_NAME ' ), $ this ->linkID [$ linkNum ]);
80+ if (mysql_errno ($ this ->linkID [$ linkNum ])){
81+ throw_exception (mysql_error ($ this ->linkID [$ linkNum ]));
82+ }
83+ $ this ->is_spare =true ;
84+ }else {
85+ throw_exception ($ errStr );
86+ }
87+ //标记使用备用数据库状态
88+ }else {
89+ //[sae] 短信预警
90+ if (C ('SMS_ON ' )) Sms::send ('数据库连接时出错,请在SAE日志中心查看详情 ' , $ errStr ,Sms::MYSQL_ERROR );
91+ throw_exception ($ errStr );
92+ }
7593 }
7694 $ dbVersion = mysql_get_server_info ($ this ->linkID [$ linkNum ]);
7795 if ($ dbVersion >= '4.1 ' ) {
@@ -152,6 +170,13 @@ public function query($str) {
152170 +----------------------------------------------------------
153171 */
154172 public function execute ($ str ) {
173+ //[sae] 判断是否开启了备用数据库
174+ if ($ this ->is_spare && !C ('SPARE_DB_WRITEABLE ' )){
175+ $ this ->error ='mysql out of quota and spare db not writeable ' ;
176+ if (C ('SPARE_INFO_FUNCTION ' ))
177+ call_user_func (C ('SPARE_INFO_FUNCTION ' ));
178+ return false ;
179+ }
155180 $ this ->initConnect (true );
156181 if ( !$ this ->_linkID ) return false ;
157182 $ this ->queryStr = $ str ;
0 commit comments