Jump to content

Mysql Database Connection


Bonsai

Recommended Posts

Hey Peeps,

I'm having a little problem with my Mysql Database connection.

If the server just started, it works fine. But if I restart my database resource, it won't connect anymore.

I'm also using destroyElement on the connection in the onResourceStop event of that resource.

Does anyone know what the problem is?

Bonsai

Link to comment
function Database.main() 
  
    Database.connection = dbConnect("mysql", "dbname=**;host=**" ,"**", "**") 
  
    if not Database.connection then 
     
        Database.connection = dbConnect("mysql", "dbname=**;host=**" ,"**", "**") 
         
        if not Database.connection then 
     
            outputDebugString("Database Connection failed!") 
             
        end 
         
    else 
         
        outputDebugString("Database Connection successful!") 
         
    end 
     
end 
addEventHandler("onResourceStart", resourceRoot, Database.main) 
  
  
function Database.kill() 
  
    if isElement(Database.connection) then destroyElement(Database.connection) end 
  
    outputDebugString("Database Connection killed!") 
     
end 
addEventHandler("onResourceStop", resourceRoot, Database.kill) 
  
  
function getConnection() 
     
    return Database.connection 
     
end 

Thats all.

The last function is exported to be used by other resources.

Link to comment

My guess is you need to put share param to 1.

options : List of key=value pairs separated by semicolons. Supported keys are:

share which can be set to 0 or 1. (Default value for SQLite is "share=1", for MySQL is "share=0"). When set to 1, the connection is shared and will be used by other calls to dbConnect with the same host string.

Link to comment

dbConnect is only used once by that resource. Other resources just use the connection.

But I discovered another problem with this now.

After some hours, the queries started to fail and the Error was "Expected db-connection, got marker".

I have no idea how that could happen...

Bonsai

Link to comment

Weird. Really very weird.

  
function Database.main() 
  
    Database.connection = dbConnect("mysql", "dbname=**;host=**" ,"**", "**") 
  
    if not Database.connection then 
    
        Database.connection = dbConnect("mysql", "dbname=**;host=**" ,"**", "**") 
        
        if not Database.connection then 
    
            outputDebugString("Database Connection failed!") 
            
        end 
        
    else 
        
        outputDebugString("Database Connection successful!") 
        
    end 
    
end 
addEventHandler("onResourceStart", resourceRoot, Database.main) 
  
  
function Database.kill() 
  
    if isElement(Database.connection) then destroyElement(Database.connection) Database.connection = nil end 
  
    outputDebugString("Database Connection killed!") 
    
end 
addEventHandler("onResourceStop", resourceRoot, Database.kill) 
  
  
function getConnection() 
    
    return Database.connection 
    
end 
  

Try this tho

Link to comment
After some hours, the queries started to fail and the Error was "Expected db-connection, got marker".

That will be problem in another script where you set exported database connection to variable, you probably assigned marker to that variable.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...