Page 1 of 1

database commits and user Disconnect

Posted: 20 Feb 2012, 14:27
by gofa
Hi, I need some advice no best way to update my user data base,

In my system, i use a custome login extension to to auth my client and pull out the saved user profile data from my data base,
e.g joined date, time spent playing, number of games played, games won, games lost, Credit's, ect...

while the user is playing, my extensions are updating my user property object, e.g
PlayerData pd = (PlayerData) user.getProperty(PlayerData.PLAYER_DATA);
pd.incGamesPlayed();
ect....

when the user exits or disconnects I update/commit to the database. this gives me 2 data base updates for ever user, i think this is a good thing ?

When the user in in a room playing a game and then dissconects / crashes out say by closing this broswed,
I want my room disconect extension to update the user.getProperty(PlayerData.PLAYER_DATA); object (in RAM),
and then using the Zone disconect extension to simpley commit the player data object to the data base.

But Smart fox fires the Zone disconect first and then the room disconect second, giving me a problem, how to solve this situation ??

if a user is not in a room and disconects, the zone disconect extension updates the data base nicely and all is ok.
because when a user leaves a room normaly, I update the user.getProperty(PlayerData.PLAYER_DATA); object

Re: database commits and user Disconnect

Posted: 21 Feb 2012, 08:08
by gofa
why dose the Zone Level Disconnect event fire before a Room level Disconnect ???
if you enter a tower block then go into a flat. to exit the tower block first you must exit the flat ?

Re: database commits and user Disconnect

Posted: 21 Feb 2012, 08:41
by gofa
so if the zone disconect event fires first and then all the room disconect event's fire (one fore each room the user is in), how should i handle my data base update ?

mabey i stop listing for a disconnect at the room level,
at the zone level disconnect, I loop through all the useres room extensions casting them to a common base class and call a method on them
to handel the disconnect. after this all the custome user data will be updated and after this loop i can commit it to the database.

this looks like the only way to do it if room disconects fire after the zone disconect. Is this method the way you advise ? or is there a better way or some thing ?

Re: database commits and user Disconnect

Posted: 21 Feb 2012, 13:32
by gofa
ok so i could call this method handleInternalMessage("dis", event);
on the extension of every room a user is in,
at the zone level on a disconnect ?

it that the best way ?

Re: database commits and user Disconnect

Posted: 24 Feb 2012, 03:23
by gofa
So I handeled the problem with a Zone level extension, handling the user disconnect event and then manualy notifing all the rooms the user is in that the dissconect happened. with internal message "END". when the room handels this message is updates some variables and stuff (my player Data)


public void handleServerEvent(ISFSEvent event) throws SFSException{
User user = (User) event.getParameter(SFSEventParam.USER);
trace("\n==="+user.getName()+" Disconects===");

@SuppressWarnings("unchecked")
Map<Room, Integer> m = (Map<Room,Integer>) event.getParameter(SFSEventParam.PLAYER_IDS_BY_ROOM);
Set<Room> s = m.keySet();
Iterator<Room> i = s.iterator();
while(i.hasNext()){
trace("\n===ROOM from event Room MAP===");
Room r = i.next();
r.getExtension().handleInternalMessage("END", event);
}

PlayerData pd = (PlayerData) user.getProperty(PlayerData.PLAYER_DATA);
Connection connection=null;
try{
IDBManager dbManager = getParentExtension().getParentZone().getDBManager();
connection = dbManager.getConnection();
pd.playerExitCommit(connection, user);
}catch(SQLException e){
trace("DisconectEvent Sql Error "+"uid="+pd.uid+" ip="+user.getIpAddress()+" name="+user.getName()+" error = "+e.getMessage()+" "+e);
}finally{
if(connection != null){
try{
connection.close();
}catch(SQLException e){trace(e);}
}
}
}




dose this look sensible to you ?
it would of course been easuer if the room level disconnect was fires before the zone level dissconnect, or am I still missing some thing ???

it would be nice if some one could surest a better way or how they handel this kind of thing.
all so any comment on data base update strageties would be great,

I am doing 1 read at longin and and 1 wright at log out, so DB access kept to a min,
however all user data is in ram so any changes are lost on server crash ?
so any comments on Memery usage speed and DB hits would be good.

Thanks

Re: database commits and user Disconnect

Posted: 13 Mar 2012, 10:32
by Lapo
But Smart fox fires the Zone disconect first and then the room disconect second, giving me a problem, how to solve this situation ??

Events are asynchronous, I don't recommend trying to run the logic by trying to coordinate those two separate events.
You should use one of the two, not both