• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CI 3 Session Lock Ajax

#1
Hi, From this thread https://forum.codeigniter.com/thread-70845.html , I upgraded CI 2 to CI 3.1.8 and problem was solved. But I got new problem with multi ajax request. 
Example : When user switch between 2 pages. Ajax request of 1st page must be completed, then user can change to 2nd page. User can't go to 2nd page or other page i ajax request of 1st page isn't complete. (User can't wait until All ajax of 1st page complete  Sad ).

I'm using database for session storage driver. I found codes in Session_database_driver.php, these codes seem lock ajax request: 

PHP Code:
/**
     * Get lock
     *
     * Acquires a lock, depending on the underlying platform.
     *
     * @param    string    $session_id    Session ID
     * @return    bool
     */
    
protected function _get_lock($session_id)
    {
        if (
$this->_platform === 'mysql')
        {
            
$arg md5($session_id.($this->_config['match_ip'] ? '_'.$_SERVER['REMOTE_ADDR'] : ''));
            if (
$this->_db->query("SELECT GET_LOCK('".$arg."', 300) AS ci_session_lock")->row()->ci_session_lock)
            {
                
$this->_lock $arg;
                return 
TRUE;
            }

            return 
FALSE;
        }
        elseif (
$this->_platform === 'postgre')
        {
            
$arg "hashtext('".$session_id."')".($this->_config['match_ip'] ? ", hashtext('".$_SERVER['REMOTE_ADDR']."')" '');
            if (
$this->_db->simple_query('SELECT pg_advisory_lock('.$arg.')'))
            {
                
$this->_lock $arg;
                return 
TRUE;
            }

            return 
FALSE;
        }

        return 
parent::_get_lock($session_id);
    }

    
// ------------------------------------------------------------------------

    /**
     * Release lock
     *
     * Releases a previously acquired lock
     *
     * @return    bool
     */
    
protected function _release_lock()
    {
        if ( ! 
$this->_lock)
        {
            return 
TRUE;
        }

        if (
$this->_platform === 'mysql')
        {
            if (
$this->_db->query("SELECT RELEASE_LOCK('".$this->_lock."') AS ci_session_lock")->row()->ci_session_lock)
            {
                
$this->_lock FALSE;
                return 
TRUE;
            }

            return 
FALSE;
        }
        elseif (
$this->_platform === 'postgre')
        {
            if (
$this->_db->simple_query('SELECT pg_advisory_unlock('.$this->_lock.')'))
            {
                
$this->_lock FALSE;
                return 
TRUE;
            }

            return 
FALSE;
        }

        return 
parent::_release_lock();
    } 

Is there any config to affect above code ? I wan't to abort all ajax request when user switch the pages, My project have many features with multi ajax  Sad .


Thanks in advance .
Reply

#2
You should check for an Ajax call in your controllers.

SEE: CodeIgniter User's Guide - is_ajax_request()
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
Are you making synchronous (as opposed to asynchronous) ajax calls or something?
Reply

#4
(06-12-2018, 06:37 AM)yoyovolam Wrote: Hi, From this thread https://forum.codeigniter.com/thread-70845.html , I upgraded CI 2 to CI 3.1.8 and problem was solved. But I got new problem with multi ajax request. 
Example : When user switch between 2 pages. Ajax request of 1st page must be completed, then user can change to 2nd page. User can't go to 2nd page or other page i ajax request of 1st page isn't complete. (User can't wait until All ajax of 1st page complete  Sad ).

I'm using database for session storage driver. I found codes in Session_database_driver.php, these codes seem lock ajax request: 

PHP Code:
/**
 * Get lock
 *
 * Acquires a lock, depending on the underlying platform.
 *
 * @param string $session_id Session ID
 * @return bool
 */
 
protected function _get_lock($session_id)
 {
 if (
$this->_platform === 'mysql')
 {
 
$arg md5($session_id.($this->_config['match_ip'] ? '_'.$_SERVER['REMOTE_ADDR'] : ''));
 if (
$this->_db->query("SELECT GET_LOCK('".$arg."', 300) AS ci_session_lock")->row()->ci_session_lock)
 {
 
$this->_lock $arg;
 return 
TRUE;
 }

 return 
FALSE;
 }
 elseif (
$this->_platform === 'postgre')
 {
 
$arg "hashtext('".$session_id."')".($this->_config['match_ip'] ? ", hashtext('".$_SERVER['REMOTE_ADDR']."')" '');
 if (
$this->_db->simple_query('SELECT pg_advisory_lock('.$arg.')'))
 {
 
$this->_lock $arg;
 return 
TRUE;
 }

 return 
FALSE;
 }

 return 
parent::_get_lock($session_id);
 }

 
// ------------------------------------------------------------------------

 /**
 * Release lock
 *
 * Releases a previously acquired lock
 *
 * @return bool
 */
 
protected function _release_lock()
 {
 if ( ! 
$this->_lock)
 {
 return 
TRUE;
 }

 if (
$this->_platform === 'mysql')
 {
 if (
$this->_db->query("SELECT RELEASE_LOCK('".$this->_lock."') AS ci_session_lock")->row()->ci_session_lock)
 {
 
$this->_lock FALSE;
 return 
TRUE;
 }

 return 
FALSE;
 }
 elseif (
$this->_platform === 'postgre')
 {
 if (
$this->_db->simple_query('SELECT pg_advisory_unlock('.$this->_lock.')'))
 {
 
$this->_lock FALSE;
 return 
TRUE;
 }

 return 
FALSE;
 }

 return 
parent::_release_lock();
 } 

Is there any config to affect above code ? I wan't to abort all ajax request when user switch the pages, My project have many features with multi ajax  Sad .


Thanks in advance .

You Problem: https://www.codeigniter.com/user_guide/l...oncurrency
Reply

#5
Switched file based sessions some time ago with a view to move on Redis/Memcached sessions down the line, never got around to it. Never had any issues with sessions since, tho I wouldn't say we really use AJAX etc heavily, so can't say if it causes race-condition issues.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2018 MyBB Group.