Game server links to database, how many database connection should use? The data update works like this: update role object to database every X minutes, commit all updates every Y minutes.
There are 3 ways:
- Only one database connection
- One worker thread one database connection and set transaction level to "Read Uncommitted". Worker thread loop all roles in it to do update & commit periodically
- One game session one database connection and set transaction level to "Read Uncommitted". A dedicated thread to loop all connections to do update & commit periodically
Method 1 has a problem which database connection object needs shared by multiple worker threads, for JDBC, connection class is thread safe, but I don't know what about C++ such as MySql driver. If it have to use mutex to make thread safe, there will be a performance degrade: all worker threads have to synchronize their data frequently.
Method 2 has a problem to update old data. One map in game server run in dedicated worker thread. For example, role A in map 1 which compute by thread 1. role A transport to map 2 which compute by thread 2. This might cause thread 1's commit call after thread 2's commit call, so the older update which already executed in thread 1 commit after thread 2's commit.
Method 3 seems is a waste of system resource. Because in MMO there is no need to be transaction isolation, any change in a game session should be immediately seen by other sessions. In contrast with web app, so many connections has litte meaning. However the "Read Uncommitted" isolation level can let other session read data which has not committed yet.
I forget there is a thing called connection pool. Generally it is thread safe implemented so I do not need to care synchronization problem anymore. And the connection number is dynamic so I can leave problem of how many connection number to it. Only I do is get a connection from it. So, method 1 & 3 both practical.