Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚠Caching won't work with replication enabled #5919

Closed
matiasmir opened this issue Apr 20, 2020 · 2 comments · Fixed by #7694
Closed

⚠Caching won't work with replication enabled #5919

matiasmir opened this issue Apr 20, 2020 · 2 comments · Fixed by #7694

Comments

@matiasmir
Copy link

Issue type:

[ ] question
[ x] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[x ] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[x ] latest
[ ] @next
[ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:
Hi everyone, I've configured typeorm with replication using two instances, a master(read-write) and a slave(read), so if my understading is correct all the select/get queries are executed in the slave instance and all the updates/set/insert/etc are executed in the master db but today I've enabled caching for my db and it doesn't work, it shows the following error when I try to get something from the db:

{ error: cannot execute INSERT in a read-only transaction at Connection.parseE (/app/node_modules/pg/lib/connection.js:604:13) at Connection.parseMessage (/app/node_modules/pg/lib/connection.js:403:19) at Socket.<anonymous> (/app/node_modules/pg/lib/connection.js:123:22) at Socket.emit (events.js:198:13) at Socket.EventEmitter.emit (domain.js:448:20) at addChunk (_stream_readable.js:287:12) at readableAddChunk (_stream_readable.js:268:11) at Socket.Readable.push (_stream_readable.js:223:10) at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17) name: 'error', length: 118, severity: 'ERROR', code: '25006', detail: undefined, hint: undefined, position: undefined, internalPosition: undefined, internalQuery: undefined, where: undefined, schema: undefined, table: undefined, column: undefined, dataType: undefined, constraint: undefined, file: 'utility.c', line: '246', routine: 'PreventCommandIfReadOnly' }

I suppose that this is being caused when a get request is received on my API and then typeorm selects the slave database in order to execute the query and then it tries to do a INSERT to chache the results using the slave instance and because it is READ ONLY it throws that error, so I guess that this is a bug, correct me if I'm wrong, thanks in advance!

@matiasmir matiasmir changed the title Caching won't work with replication enabled ⚠Caching won't work with replication enabled Apr 21, 2020
@artemkonovalov-ss
Copy link

artemkonovalov-ss commented Nov 9, 2020

Same for MySQL. Cache is not working

That doesn't produce errors but it hits DB per each request, but not a cache

@madeindjs
Copy link
Contributor

I created a dedicated repository to reproduce the error using Docker to mount a master/slave environment

git clone https://github.com/madeindjs/typeorm_bug
cd typeorm_bug
docker-compose up

This will produce the following error

QueryFailedError: cannot execute INSERT in a read-only transaction
    at new QueryFailedError (/usr/src/app/src/error/QueryFailedError.ts:9:9)
    at PostgresQueryRunner.<anonymous> (/usr/src/app/src/driver/postgres/PostgresQueryRunner.ts:228:19)
    at step (/usr/src/app/node_modules/tslib/tslib.js:143:27)
    at Object.throw (/usr/src/app/node_modules/tslib/tslib.js:124:57)
    at rejected (/usr/src/app/node_modules/tslib/tslib.js:115:69)
    at processTicksAndRejections (internal/process/task_queues.js:93:5) {
  length: 118,
  severity: 'ERROR',
  code: '25006',
  detail: undefined,
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'utility.c',
  line: '246',
  routine: 'PreventCommandIfReadOnly',
  query: 'INSERT INTO "query-result-cache"("identifier", "query", "time", "duration", "result") VALUES (DEFAULT, $1, $2, $3, $4)',
  parameters: [
    'SELECT name FROM "user" "u" -- PARAMETERS: []',
    1622197257656,
    2000,
    '[{"name":" toto"},{"name":" toto"}]'
  ]
}

madeindjs added a commit to madeindjs/typeorm that referenced this issue May 28, 2021
madeindjs added a commit to madeindjs/typeorm that referenced this issue May 31, 2021
madeindjs added a commit to madeindjs/typeorm that referenced this issue May 31, 2021
madeindjs added a commit to madeindjs/typeorm that referenced this issue May 31, 2021
madeindjs added a commit to madeindjs/typeorm that referenced this issue Jun 1, 2021
madeindjs added a commit to madeindjs/typeorm that referenced this issue Nov 13, 2021
pleerock pushed a commit that referenced this issue Nov 16, 2021
* FIX Insert cache only on master connection

close #5919

* FIX release queryRunner created
HeartPattern pushed a commit to HeartPattern/typeorm that referenced this issue Nov 29, 2021
* FIX Insert cache only on master connection

close typeorm#5919

* FIX release queryRunner created
HeartPattern pushed a commit to HeartPattern/typeorm that referenced this issue Nov 29, 2021
* FIX Insert cache only on master connection

close typeorm#5919

* FIX release queryRunner created
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants