?
Solved

Pear, Oracle, Persistent connection

Posted on 2003-02-19
4
Medium Priority
?
1,753 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

770 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