#MYSQL connection

28 messages · Page 1 of 1 (latest)

queen marshBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

tidal hawk
#

thats an expected behavior, because nextjs is serverless. So there is not server, that saves any pools, details, states, ...

autumn panther
tidal hawk
autumn panther
tidal hawk
# autumn panther But, wouldn't that create thousands of mysql connection labeled as sleep? And ho...

yea, that would happen. Many (me included) build a singleton to resuse one connection in the same request. So you can use one connection for one whole request, that can contain many requests to the database. And I am using a idle timeout, to close them automatically, when they are not used. My current idle timeout is at 1 minute, so my serverless functions can open a connection, can do 1 minutes (just execution time) their stuff inside it and after that it will be automatically closed. Of course you can also close them manuelly after you done

autumn panther
#

Ahh yeah, a few other things call to the same database, all mysql2 and pooled. Just not nextJS, would changing the idle time out effect that?

#

I am also pretty much new to the term singleton,

    static #instance: Singleton;

    /**
     * The Singleton's constructor should always be private to prevent direct
     * construction calls with the `new` operator.
     */
    private constructor() { }

    /**
     * The static getter that controls access to the singleton instance.
     *
     * This implementation allows you to extend the Singleton class while
     * keeping just one instance of each subclass around.
     */
    public static get instance(): Singleton {


//MySQL create connection with( pooling? ) would be here ?
        if (!Singleton.#instance) {
            Singleton.#instance = new Singleton();
        }

        return Singleton.#instance;
    }


}```
autumn panther
tidal hawk
autumn panther
#

Yeah I got confused about that too tbf post said it was tsx.
So I'd assume I'll have to get rid of pooling, and just export the mysql connection where you make the drizzle call, then import the new singleton file and just call to db?

autumn panther
tidal hawk
#

yea, the idle timeout is a db setting and .close() depends on your backend and software that you are using. So check if there is any .close() function or similar

autumn panther
tidal hawk
autumn panther
#

(I am very new to next and serverless in general, and never had issues like this, my apologies)

autumn panther
tidal hawk
tidal hawk
autumn panther
tidal hawk
autumn panther
tidal hawk
autumn panther
#

Yeah, thanks

tidal hawk
queen marshBOT
#
✅ Success!

This question has been marked as answered! If you have any other questions, feel free to create another post

Jump to answer
tidal hawk
autumn panther
#

Thansk for the help^