StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
DbInit.cxx
1 /***************************************************************************
2  *
3  * $Id: DbInit.cxx,v 1.7 2007/05/16 22:47:54 deph Exp $
4  *
5  * Author: S. Vanyashin
6  ***************************************************************************
7  *
8  * Description: Initializes connection to database
9  *
10  ***************************************************************************
11  *
12  * $Log: DbInit.cxx,v $
13  * Revision 1.7 2007/05/16 22:47:54 deph
14  * Replaced cerr with LOG_ERROR <<endm; for logger
15  *
16  * Revision 1.6 2003/09/02 17:55:35 perev
17  * gcc 3.2 updates + WarnOff
18  *
19  * Revision 1.5 2000/01/14 14:49:10 porter
20  * set verbose level for checking, added $Id & $Logs, & made node container
21  * more robust for interactions with StDbLib
22  *
23  *
24  **************************************************************************/
25 //this function check if it is possible to connect to database server
26 #include <Stiostream.h>
27 #include "mysql.h"
28 #include "StMessMgr.h"
29 
30 extern "C" int DbInit(const char * dbName)
31 {
32 
33 MYSQL mysql;
34 
35 mysql_init(&mysql);
36 
37 //set timout in seconds for bnl.local domain
38 
39 #ifndef __sun
40 //on sun:
41 //Program received signal SIGBUS, Bus error.
42 //0xed737d68 in mysql_options ()
43 
44 mysql_options(&mysql,MYSQL_OPT_CONNECT_TIMEOUT,"2");
45 
46 #endif
47 // Try to establish a connection to the MySQL database engine
48 
49 const char *database=dbName;
50 //only db1 is visible from rcas0202 machine
51 const char *dbHost="db1.star.bnl.gov";
52 //char *dbHost="duvall.star.bnl.gov";
53 //mysql_real_connect(MYSQL *mysql, const char *host, const char *user, const char *passwd, const char *db, uint port, const char *unix_socket, uint client_flag)
54 
55 if (!mysql_real_connect(&mysql,dbHost,"","",database,0,NULL,0))
56  {
57  LOG_ERROR << "Failed to connect to database: "<<database<< endm;
58  LOG_ERROR << "MySQL ERROR: " << mysql_error(&mysql) << endm;
59  return 1;
60  }
61 
62 // We are done with the connection, call mysql_close() to terminate it
63 mysql_close(&mysql);
64 
65 return 0;
66 
67 }
68 
69