Using second databaseconnection in 2 widgets

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Removed-836727
    Banned by User Request
    • Apr 2006
    • 1274

    [CMS] Using second databaseconnection in 2 widgets

    Hi.

    I've created a widget which gets the data from an other database.
    So i've created a second db connection in the widget.

    Now i want 2 or more widgets which gets there data from the other connection.

    What would be the best way to do that?
    ATM i open the connection, send the query to get the data, close the connection.
    In the second widget the same thing is happening.

    Now i thought to create a singleton class for the db connection, so i don't have to open & close it in every widget.
    Would that be better?
    Where should i than close the connection?
  • Edwin Brown
    Former vBulletin Developer
    • Mar 2009
    • 1393
    • 5.5.x

    #2
    Personally I would do that-make a singleton class so the connection is maintained. That's how we do it.
    Please- I'm not tech support. Don't send your problem reports to me unless I've asked you to.

    Comment

    • Removed-836727
      Banned by User Request
      • Apr 2006
      • 1274

      #3
      OK thx.
      Last edited by Removed-836727; Tue 4 May '10, 9:26am.

      Comment

      • Removed-836727
        Banned by User Request
        • Apr 2006
        • 1274

        #4
        That's my code now
        PHP Code:
        class ragtek_seconddatabaseconnection
        {
            private static 
        $instance null;

            
        /**
             * Constructor protected to enforce singleton use.
             */
            
        protected function  __construct() {
            }

            
        /**
             * returns the databaseconnection
             * @return <vB_Database> Database Object
             */
            
        public static function Instance()
            {
                if (
        self::$instance === null)
                {
                    
        self::$instance = new vB_Database(vB::$vbulletin);
                    
        self::$instance->connect('oscommerce''localhost'null'shopdb''');
                }
                return 
        self::$instance;
            }

            
        /**
             * protected to enforce singleton use
             */
            
        protected function  __clone() {
            }

        Any optimisation tips?

        And to close the db connection, i've created a plugin at hook global_complete
        PHP Code:
        if (class_exists('ragtek_seconddatabaseconnection'))
        {
        ragtek_seconddatabaseconnection::Instance()->close();

        Last edited by Removed-836727; Tue 4 May '10, 6:10pm.

        Comment

        widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
        Working...