Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Pear, Oracle, Persistent connection

Posted on 2003-02-19
4
Medium Priority
?
1,764 Views
Last Modified: 2012-05-04
I'm using pear as an abstraction-layer while working with a Oracle-db.

I would like to open a persistent connection. My code is like this:

     /* Oracle-connection */
     require_once 'DB.php';
     $user = 'myuser';
     $pass = 'mypassword';
     $host = 'myhost.domain.no';
     $db_name = 'myhost.domain.no;
     
     // Data Source Name: This is the universal connection string
     $dsn = "oci8://$user:$pass@$host";
     
     // DB::connect will return a PEAR DB object on success
     // or an PEAR DB Error object on error
     $db = DB::connect($dsn);
     
     // Alternatively: $db = DB::connect($dsn, true); // to get a persistent connection...
     
     // With DB::isError you can differentiate between an error or
     // a valid connection.
     if (DB::isError($db))
     {    
          die ($db->getMessage());
     }

     $db->setFetchMode(DB_FETCHMODE_ASSOC);

The documentation tells me to change

$db = DB::connect($dsn);

to

$db = DB::connect($dsn,TRUE);

to open a persistent connection, BUT:

When a new page is loaded, how could I retreve the allready opened connection? I guess that if I use it like above i would get a lot of opened connections, in other words a new connectio for each page, until max number of connections is reached.

Any thoughts?

Batalf
0
Comment
Question by:Batalf
  • 3
4 Comments
 
LVL 1

Accepted Solution

by:
s-maxim earned 400 total points
ID: 7988065

Basically, apache holds your session on its own. In fact, it is useless closing the connection due the fact that the OCILogoff(), internally, is entirely commented (always because it is up to Zend Engine to lisrten to Apache and close any opened external connections).

However, if you want to be very much sure, you can serialize and unserialize the Connection ID, or better say the whole PEAR object and pass it in the session.

Consider this code:

----page:

<?php

// Define the class anyway
require_once 'DB.php';

// Start your session
session_start();

// Check whether the session is archived

// If nothing is memorized, do it for the first time:
if(!isset($_SESSION['PEAR'])) {
    $user = 'myuser';
    $pass = 'mypassword';
    $host = 'myhost.domain.no';
    $db_name = 'myhost.domain.no;
    
    $dsn = "oci8://$user:$pass@$host";
    
    $db = DB::connect($dsn, True);
}

// Otherwise, retrieve it from the session
else {
    $db = unserialize($_SESSION['PEAR']);
}

// Use $db object any way you wish....

...

// Archive your class instance for the next page
$_SESSION['PEAR'] = serialize($db);

?>


I haven't tested this very code, but have done such staff in past and it should perfectly work.

It is not reccomended though, as Apache is holding the Oracle session anyway, so you can simply connect to it and worry about nothing at all.

Try it on your own and see what works for you.

Hope it works for you (and that you accept it for the whole 100 points for me to freely spend in C/C++ section :))

Maxim Maletsky
maxim@php.net
0
 
LVL 32

Author Comment

by:Batalf
ID: 7988885
I think I'm on the right track, but I'm getting this error:

PHP Fatal error:  The script tried to execute a method or access a property of an incomplet
e object. Please ensure that the class definition <b>db_oci8</b> of the object you are trying to operate on was loaded _
before_ the session was started ..... on line 42

on this code:

     require_once 'DB.php';
     

     // Start your session
     session_start();

     // Check whether the session is archived
     
     // If nothing is memorized, do it for the first time:
     if(!isset($_SESSION['PEAR'])) {
          $user = 'user';
          $pass = 'password';
          $host = 'myhost.domain.no';
          $db_name = 'myhost.domain.no';
       
         $dsn = "oci8://$user:$pass@$host";
       
         $db = DB::connect($dsn, True);
         
         
          // Alternatively: $db = DB::connect($dsn, true); // to get a persistent connection...
         
          // With DB::isError you can differentiate between an error or
          // a valid connection.
          if (DB::isError($db))
          {    
               die ($db->getMessage());
          }
         
          $db->setFetchMode(DB_FETCHMODE_ASSOC);
         
          $_SESSION['PEAR'] = serialize($db);          
     }
     
     // Otherwise, retrieve it from the session
     else {
        $db = unserialize($_SESSION['PEAR']);
     }
I also have an other question, and I will gladly increase the points of this question to 200 or 300 points if you could help me out with that too:

I'm having a problem inserting text with a length larger than 4K into Oracle using pear. The SQL-query fail. The text-field is a CLOB(Character Large Object).
0
 
LVL 32

Author Comment

by:Batalf
ID: 7989095
It seems that my first question is solved using your code. I had to move the session_start() call right above $_SESSION['PEAR'].

Do you have an answer to the other question too? (The problem with the LOB-fields).
0
 
LVL 32

Author Comment

by:Batalf
ID: 7989100
I posted the other question earlier today at the Oracle-forum.

http://www.experts-exchange.com/Databases/Oracle/Q_20522157.html

0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question