GithubHelp home page GithubHelp logo

Comments (13)

sfabriece avatar sfabriece commented on June 5, 2024 1

Ahh I see. Let me play around and see what I can come up with.

Still interested in how you guard against db lost connection?

from hapi-plugin-mysql.

AdriVanHoudt avatar AdriVanHoudt commented on June 5, 2024

It mostly follows the hapi style guide. Just like server.app, request.app is made to put anything in you want.

I did went back and looked at the api (http://hapijs.com/api#request-properties) and it does seem like it would be cleaner exposed as request.plugins['hapi-plugin-mysql'].db but that seems a bit convoluted.

Another option would be to use http://hapijs.com/api#serverdecoratetype-property-method-options so you could do request.getDb(), request.connection(), request.db() or request.getConnection() or something which would allow just getting a connection when you need it. But you would have to manually close it like when using server.getDb() (unless I can find some way to store the connection in the plugin and close on tail somehow)

If you have any ideas/opinions on these options I would gladly listen to them. If there is going to be any change it would probably be the last one personally.

from hapi-plugin-mysql.

sfabriece avatar sfabriece commented on June 5, 2024

I totally agree on the last one, I'll try and see if there is a simple way of getting the last one to work without much change on the way it is currently being done.

By the way, how do you handle db disconnects in your code?

from hapi-plugin-mysql.

AdriVanHoudt avatar AdriVanHoudt commented on June 5, 2024

PR's welcome ;) if you can find a way to auto close the connection I can add the request method and it won't even be a breaking change :D

from hapi-plugin-mysql.

sfabriece avatar sfabriece commented on June 5, 2024

I was looking at mysql modules docs, and it seems that you can use the pool directly without concerns of releasing the connection.
Would there been a downside of adding the pool to the request and removing it on the tail?

from hapi-plugin-mysql.

AdriVanHoudt avatar AdriVanHoudt commented on June 5, 2024

This plugin already uses a pool, you do have to tell the mysql module you are done with a connection so the pool knows which connections are free to use again

from hapi-plugin-mysql.

sfabriece avatar sfabriece commented on June 5, 2024

This doesn't require a release of the connection, as it is on the pool directly.

Pooling connections

Use pool directly.

var mysql = require('mysql');
var pool  = mysql.createPool({
  connectionLimit : 10,
  host            : 'example.org',
  user            : 'bob',
  password        : 'secret'
});

pool.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
  if (err) throw err;

  console.log('The solution is: ', rows[0].solution);
});

from hapi-plugin-mysql.

AdriVanHoudt avatar AdriVanHoudt commented on June 5, 2024

That uses a connection per query (https://github.com/felixge/node-mysql/blob/1720920f7afc660d37430c35c7128b20f77735e3/lib/Pool.js#L184), while the plugin now gives you a connection per request.

from hapi-plugin-mysql.

AdriVanHoudt avatar AdriVanHoudt commented on June 5, 2024

Not sure what you mean by db lost connection?

from hapi-plugin-mysql.

sfabriece avatar sfabriece commented on June 5, 2024

I mean how do you recover from the database being disconnect from your server?

from hapi-plugin-mysql.

AdriVanHoudt avatar AdriVanHoudt commented on June 5, 2024

atm this plugin throws since it assumes that you always have a connection if the first one works.
I could provide a function that get's called on error so you can gracefully handle it. Now with a pool if 1 connection fails it will remove it from the pool and try another one. see https://github.com/felixge/node-mysql#server-disconnects

from hapi-plugin-mysql.

sfabriece avatar sfabriece commented on June 5, 2024

It might be of interest, So ar I haven't thought of a way to handle the db server being disconnected.
I think your way might be the correct way to do it.

from hapi-plugin-mysql.

AdriVanHoudt avatar AdriVanHoudt commented on June 5, 2024

When I have time I'll look into adding this!

from hapi-plugin-mysql.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.