DB Error: Duplicate entry 'xxxxxx' for key 'sessions_pkey'

Hi

I have the same problem if i delete the entry from the sessions table, it works for that instance, in the next click on any page or refresh of the ojs, it gives the same error.

what could be the issue?

I tried with the php_value session.auto.start 0; through .htaccess but i do not see any difference.

Appreciate your help on this.

Hi @Umadhar,

I would suggest checking your phpinfo output to ensure that the session auto-start is actually disabled by your .htaccess mechanism.

Regards,
Alec Smecher
Public Knowledge Project Team

Thank you for your immediate reply. Here is the phpinfo of my hosting environment:

http://tangentinfotech.com.fozzyhost.com/phpinfo.php

session.auto_start is already Off.

Why the duplicate issue is coming up? Any settings needs to be done? I am having this issue when i am moving the ojs from one hosting to another hosting.

Please guide and save me from this issue.

Thanks in advance and appreciate your help.

Hi @Umadhar,

Have you checked your PHP error log for any relevant entries – especially anything besides the message you already quoted?

Regards,
Alec Smecher
Public Knowledge Project Team

@asmecher ,

Please find the log here, Did not get the exact issue to fix it :

[Sat Jun 10 13:47:23.673742 2017] [lsapi:notice] [pid 467697] [client 122.172.129.23:35965] [host ayushdhara.in] Backend log: ojs2: DB Error: Duplicate entry ‘cuff3emf3rirk9qtkubgnlp6o5’ for key ‘sessions_pkey’\n
[Sat Jun 10 13:48:11.410799 2017] [lsapi:notice] [pid 470967] [client 122.172.129.23:37678] [host ayushdhara.in] Backend log: ojs2: DB Error: Duplicate entry ‘cuff3emf3rirk9qtkubgnlp6o5’ for key ‘sessions_pkey’\n, referer: AYUSHDHARA
[Sat Jun 10 13:49:07.555685 2017] [lsapi:notice] [pid 459236] [client 122.172.129.23:39677] [host ayushdhara.in] Backend log: ojs2: DB Error: Duplicate entry ‘cuff3emf3rirk9qtkubgnlp6o5’ for key ‘sessions_pkey’\n, referer: Archives | AYUSHDHARA
[Sat Jun 10 13:49:22.096643 2017] [lsapi:notice] [pid 475945] [client 122.172.129.23:40242] [host ayushdhara.in] Backend log: ojs2: DB Error: Duplicate entry ‘cuff3emf3rirk9qtkubgnlp6o5’ for key ‘sessions_pkey’\n, referer: Archives | AYUSHDHARA
[Sat Jun 10 13:49:25.105682 2017] [lsapi:notice] [pid 468020] [client 122.172.129.23:40359] [host ayushdhara.in] Backend log: ojs2: DB Error: Duplicate entry ‘cuff3emf3rirk9qtkubgnlp6o5’ for key ‘sessions_pkey’\n, referer: Archives | AYUSHDHARA
[Sat Jun 10 13:49:38.289439 2017] [lsapi:notice] [pid 467697] [client 122.172.129.23:40799] [host ayushdhara.in] Backend log: ojs2: DB Error: Duplicate entry ‘cuff3emf3rirk9qtkubgnlp6o5’ for key ‘sessions_pkey’\n, referer: Vol 3, issue 5: September - October 2016 | AYUSHDHARA
2017/06/10 13:51:57 [warn] 456084#456084: *55059038 an upstream response is buffered to a temporary file /var/cache/nginx/proxy_temp/7/17/0000328177 while reading upstream, client: 66.249.69.207, server: ayushdhara.in, request: “GET /index.php/ayushdhara/article/download/154/172 HTTP/1.1”, upstream: “http://127.0.0.1:8080/index.php/ayushdhara/article/download/154/172”, host: “ayushdhara.in”
[Sat Jun 10 13:51:57.970529 2017] [lsapi:error] [pid 473274] [client 66.249.69.207:45924] [host ayushdhara.in] Backend fatal error: PHP Fatal error: Call to a member function getRequest() on null in /var/www/tangenti/data/www/ayushdhara.in/plugins/generic/usageStats/UsageStatsPlugin.inc.php on line 223\n
[Sat Jun 10 14:03:11.185643 2017] [lsapi:notice] [pid 504503] [client 188.233.132.1:41422] [host ayushdhara.in] Backend log: ojs2: DB Error: Duplicate entry ‘snmrq7smhgn313fq0gfejf0bv4’ for key ‘sessions_pkey’\n, referer: Login | AYUSHDHARA
[Sat Jun 10 14:03:11.392052 2017] [lsapi:notice] [pid 494521] [client 188.233.132.1:41428] [host ayushdhara.in] Backend log: ojs2: DB Error: Duplicate entry ‘snmrq7smhgn313fq0gfejf0bv4’ for key ‘sessions_pkey’\n, referer: http://ayushdhara.in/

I have two sites behaving this way right now including this and i do not have a solutions, What is causing this issue, why does it say Duplicate entry? Do we need to make sessions_id to be altered to not Primary Key? or is there anything in the code that we need to do? When the same session id is found for a request/refresh it should update the existing one but why it is throwing the duplicate entry as a new insert of the same session id. why it is happening? Please Please help me on this.

@asmecher,

I enabled the debug on and show stats on and here is the stack trace, It seems it is trying to insert, not sure whether it is the expected behaviour:

(mysql): SET NAMES ‘utf8’
(mysql): INSERT INTO sessions (session_id, ip_address, user_agent, created, last_used, remember, data, domain) VALUES (‘cuff3emf3rirk9qtkubgnlp6o5’, ‘122.172.129.23’, ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36’, 1497097561, 1497097561, 0, ‘’, ‘ayushdhara.in’)
1062: Duplicate entry ‘cuff3emf3rirk9qtkubgnlp6o5’ for key ‘sessions_pkey’
ADOConnection._Execute(INSERT INTO sessions
(session_id, ip_address, user_agent, created, last_used, remember, data, domain)
VALUES
('cuff3…) % line 845, file: adodb.inc.php
ADOConnection.Execute(INSERT INTO sessions
(session_id, ip_address, user_agent, created, last_used, remember, data, domain)
VALUES
(?, ?, …, Array[8]) % line 226, file: DAO.inc.php
DAO.update(INSERT INTO sessions
(session_id, ip_address, user_agent, created, last_used, remember, data, domain)
VALUES
(?, ?, …, Array[8]) % line 88, file: SessionDAO.inc.php
SessionDAO.insertSession(Object:Session) % line 91, file: SessionManager.inc.php
SessionManager.SessionManager(Object:SessionDAO, Object:Request) % line 130, file: SessionManager.inc.php
DB Error: Duplicate entry ‘cuff3emf3rirk9qtkubgnlp6o5’ for key ‘sessions_pkey’

Stack Trace:

File: /var/www/tangenti/data/www/ayushdhara.in/lib/pkp/classes/session/SessionDAO.inc.php line 88
Function: DAO->update(“INSERT INTO sessions (session_id, ip_address, user_agent, cr…”, Array(8))

File: /var/www/tangenti/data/www/ayushdhara.in/lib/pkp/classes/session/SessionManager.inc.php line 91
Function: SessionDAO->insertSession(Object(Session))

File: /var/www/tangenti/data/www/ayushdhara.in/lib/pkp/classes/session/SessionManager.inc.php line 130
Function: SessionManager->SessionManager(Object(SessionDAO), Object(Request))

File: /var/www/tangenti/data/www/ayushdhara.in/classes/security/Validation.inc.php line 379
Function: SessionManager->getManager()

File: /var/www/tangenti/data/www/ayushdhara.in/lib/pkp/classes/core/PKPPageRouter.inc.php line 66
Function: Validation->isLoggedIn()

File: /var/www/tangenti/data/www/ayushdhara.in/lib/pkp/classes/core/Dispatcher.inc.php line 117
Function: PKPPageRouter->isCacheable(Object(Request))

File: /var/www/tangenti/data/www/ayushdhara.in/lib/pkp/classes/core/PKPApplication.inc.php line 178
Function: Dispatcher->dispatch(Object(Request))

File: /var/www/tangenti/data/www/ayushdhara.in/index.php line 64
Function: PKPApplication->execute()

Please let me know if something can be observed here to fix the issue.

Note:
Made it as Under Maintenance for now not to show errors to customers.

Hi @Umadhar,

Hmm, this is strange behavior – I’d suggest changing session_cookie_name in config.inc.php to something else to eliminate old cookies / cookie path changes as a cause. Make sure your session_cookie_path is set appropriately – it looks like you’re behind a reverse proxy, so this may be necessary. Anytime you change something in your configuration and want to test it, make sure you’re erasing the relevant cookie from your browser.

Regards,
Alec Smecher
Public Knowledge Project Team

@asmecher,

Yes Really taking my time and killing me this issue. What if we make the session_id from sessions table makes non unique, does it harm anything.

I am trying your suggestions. What is the problem with the Reverse proxy, how do we resolve if that is the issue?

Hi @Umadhar,

The session ID needs to be unique – removing the constraint won’t resolve the problem.

If possible, I would suggest trying to access OJS without the reverse proxy being in the way. Unfortunately I can’t give you specifics on how to do this because there are many possible proxy configurations, but within our hosting environment here at SFU, it would normally mean accessing the hosting server directly (possibly at a different IP address and port than your front-facing proxy uses).

Regards,
Alec Smecher
Public Knowledge Project Team

@asmecher ,

I changed the session.save_path but it is not working but saying the folder needs to have permissions to write which i gave as 757 too. Please find the errors and stack trace on the portal directly:

http://ayushdhara.in/

With regards to Reverse proxy, if i take the dedicated IP Address, this will become direct access. If this is wrong understanding please let me know what is meant by direct OJS access?

One more question: in the sessionManager.inc.php do i need to make any chagnes to the following one at line 42:

ini_set(‘session.cookie_path’, $request->getBasePath() . ‘/’);

Attached a screenshot for your reference that the sessions are being written in the sessions/ folder as well but still the error being thrown.

Hi @Umadhar,

OJS configures PHP to use database-backed sessions (in the sessions table). It looks like your PHP configuration is for some reason still attempting to create file-based sessions. I suspect the cause of this is somewhere in your PHP configuration, but unfortunately I can’t spot it in the phpinfo() output you posted above. You might try something like StackOverflow to get hints on where to look.

Regards,
Alec Smecher
Public Knowledge Project Team

@asmecher,

Based on the session.save_path the sessions are being stored in that folder which i set as per your instruction. Just wanted to understand, when the sessions are being managed through the database-blackened sessions, why do we need to worry about the session.save_path?

What i observed is, This is storing in the database as well as in the file based, not sure why and what is happening.

There is some setting which is causing problem for the sessions to set $this->userSession in SessionManager.inc.php if we come to know about it the issue will be fixed. You might have received lot of questions on this if you know already that would be great help and save my days…

Will the dedicated IP Address works for the reverse proxy issue?

Thank you for your responses and appreciate it.

Thanks and Regards,
Umadhar

Hi @Umadhar,

The session save path isn’t the setting I referenced. I was talking about a differently-named setting in config.inc.php, not php.ini.

If you see sessions being saved in the filesystem as you attempt to access OJS, then some aspect of your PHP configuration is preventing OJS from managing its own sessions, leading to the problem you describe. It appears to be something non-standard or unusual, and I’m afraid I’m not going to be able to debug it remotely. I would suggest talking with your server admin or inquiring on Stackoverflow.

Regards,
Alec Smecher
Public Knowledge Project Team

1 Like

@asmecher,

Let me explain you:

session_cookie_name : I changed it.

session_cookie_path:
I searched the entire config.inc.php for the session.cookie_path i did not find any, i tried with path, cookie and session words search. So i set it in the .htaccess. If you think particular config parameter/variable please let me know that will help.

I do not see any change after it.

Please guide me on this…

Hi @Umadhar,

It’s session_cookie_path, not session.cookie_path.

Regards,
Alec Smecher
Public Knowledge Project Team

Hi @asmecher,

Yes, it was my typo, I made changes to session.cookie_path. only.

Any other valuable suggestions? What about on the dedicated IP address to access directly?

I am going to do new installation of the OJS 2.4.7-1 to see if there is any reverse proxy issue to see how it works and come back, So that we can nail this down.

Thanks you and have a good night.

Hi @Umadhar,

You wrote session.cookie_path again – I mean the session_cookie_path setting in your config.inc.php.

Regards,
Alec Smecher
Public Knowledge Project Team

Hi @asmecher,

Again Typo :(, I did not find session_cookie_path in the config.inc.php, we have only the session_cookie_name. Did you mean shall i set the session_cookie_path explicitly in the config.inc.php?

Even in the SessionManager.inc.php file, we have the following session parameters:

	// Configure PHP session parameters
	ini_set('session.use_trans_sid', 0);
	ini_set('session.save_handler', 'user');
	ini_set('session.serialize_handler', 'php');
	ini_set('session.use_cookies', 1);
	ini_set('session.name', Config::getVar('general', 'session_cookie_name')); // Cookie name
	ini_set('session.cookie_lifetime', 0);
	ini_set('session.cookie_path', $request->getBasePath() . '/');
	ini_set('session.cookie_domain', $request->getServerHost(null, false));
	ini_set('session.gc_probability', 1);
	ini_set('session.gc_maxlifetime', 60 * 60);
	ini_set('session.auto_start', 1);
	ini_set('session.cache_limiter', 'none');

So in the above, do we need to something for the session.cookie_path also?

Hi @gbcriado,

Look in config.TEMPLATE.inc.php for an example of how your session_cookie_path should be configured. If it’s not in your config.inc.php, perhaps that’s because you’re using a configuration file from an older release of OJS?

Regards,
Alec Smecher
Public Knowledge Project Team

@asmecher,

How are you?

Yes, i am using the old OJS 2.4.7-1 version, and i found the session_cookie_path in the OJS3 version, I will try out this option today and come back.

I tried seperately and i do not see the duplicate Sessions_Pkey issue but i am having a different issue as following, Could you please trace this out please:

(mysql): SELECT * FROM sessions WHERE session_id = ‘k7uhsgl1sudsp6c24j0o1d56q3’
(mysql): SELECT * FROM site
(mysql): SELECT v.* FROM versions v LEFT JOIN plugin_settings ps ON lower(v.product_class_name) = ps.plugin_name AND ps.setting_name = ‘enabled’ AND ((journal_id = 0) OR v.sitewide = 1) WHERE v.current = 1 AND (ps.setting_value = ‘1’ OR v.lazy_load <> 1)

Fatal error: Call to a member function getId() on null in C:\xampp\htdocs\ojs2\plugins\generic\customLocale\CustomLocalePlugin.inc.php on line 28
(mysql): UPDATE sessions SET user_id = NULL, ip_address = ‘127.0.0.1’, user_agent = ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36’, created = 1497460836, last_used = 1497461939, remember = 0, data = ‘’, domain = ‘ayush.in’ WHERE session_id = ‘k7uhsgl1sudsp6c24j0o1d56q3’