GithubHelp home page GithubHelp logo

Comments (4)

agentzh avatar agentzh commented on July 22, 2024

@njentit Have you ever put your connections into the connection pool via the set_keepalive method? If not, then your connection pool is always empty and you won't have any reused connections. In addition, you need to ensure your set_keepalive method is not ever failing by always checking its return values in your Lua code.

from lua-resty-mysql.

njentit avatar njentit commented on July 22, 2024

I'm running 1 worker_processes openresty ver. 1.7.10.1 on osx Lion with mysql 5.6. Here my nginx config, it just your sample config with addition printing get_reused_times after db:connect

location /test {
content_by_lua '
local mysql = require "resty.mysql"
local db, err = mysql:new()
if not db then
ngx.say("failed to instantiate mysql: ", err)
return
end

            db:set_timeout(1000) -- 1 sec

            local ok, err, errno, sqlstate = db:connect{
                host = "127.0.0.1",
                port = 3306,
                database = "testdb",
                user = "test",
                password = "test123",
                max_packet_size = 1024 * 1024 }

            if not ok then
                ngx.say("failed to connect: ", err, ": ", errno, " ", sqlstate)
                return
            end

            ngx.say("connected to mysql.")

            local times, err = db:get_reused_times()
            if not times then
                ngx.say("failed to get get_reused_times: ", err)
                return
            end
            ngx.say("connection reused ", times," times.") 

            local res, err, errno, sqlstate =
                db:query("drop table if exists cats")
            if not res then
                ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
                return
            end

            res, err, errno, sqlstate =
                db:query("create table cats "
                         .. "(id serial primary key, "
                         .. "name varchar(5))")
            if not res then
                ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
                return
            end

            ngx.say("table cats created.")

            res, err, errno, sqlstate =
                db:query("insert into cats (name) "
                         .. "values (\'Bob\'),(\'\'),(null)")
            if not res then
                ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
                return
            end

            ngx.say(res.affected_rows, " rows inserted into table cats ",
                    "(last insert id: ", res.insert_id, ")")

            -- run a select query, expected about 10 rows in
            -- the result set:
            res, err, errno, sqlstate =
                db:query("select * from cats order by id asc", 10)
            if not res then
                ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
                return
            end

            local cjson = require "cjson"
            ngx.say("result: ", cjson.encode(res))

            -- put it into the connection pool of size 100,
            -- with 10 seconds max idle timeout
            local ok, err = db:set_keepalive(10000, 100)
            if not ok then
                ngx.say("failed to set keepalive: ", err)
                return
            end

            ngx.say("set_keepalive success")
        ';
    }

}

set_keepalive return success, next code executed print zero result. Thanks.

from lua-resty-mysql.

agentzh avatar agentzh commented on July 22, 2024

@njentit I've tried your example on my side and it works perfectly as expected:

agentzh@w530 ~ $ curl localhost:8080/test
connected to mysql.
connection reused 0 times.
table cats created.
3 rows inserted into table cats (last insert id: 1)
result: [{"name":"Bob","id":"1"},{"name":"","id":"2"},{"name":null,"id":"3"}]
set_keepalive success

agentzh@w530 ~ $ curl localhost:8080/test
connected to mysql.
connection reused 1 times.
table cats created.
3 rows inserted into table cats (last insert id: 1)
result: [{"name":"Bob","id":"1"},{"name":"","id":"2"},{"name":null,"id":"3"}]
set_keepalive success

agentzh@w530 ~ $ curl localhost:8080/test
connected to mysql.
connection reused 2 times.
table cats created.
3 rows inserted into table cats (last insert id: 1)
result: [{"name":"Bob","id":"1"},{"name":"","id":"2"},{"name":null,"id":"3"}]
set_keepalive success

You can see that the reused times are increasing monotonically.

Maybe you have turned off the Lua code cache by configuring lua_code_cache off in your nginx.conf file? The cosocket connection pools require the Lua code cache to work across requests.

from lua-resty-mysql.

njentit avatar njentit commented on July 22, 2024

it working greats, sorry i missed that. Sorry for using issue as this actually not an issue, i'll use group for next question if any. I'm currently porting one of php project into lua+openresty.
Thanks for great work.

from lua-resty-mysql.

Related Issues (20)

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.