Refactoring Database Access in Marauroa: Difference between revisions

Jump to navigation Jump to search
Content deleted Content added
imported>Hendrik Brummermann
imported>Kymara
 
(67 intermediate revisions by 3 users not shown)
Line 1: Line 1:
At the moment database access in marauroa is designed for a single thread application. While this does work most of the time in turn based games (yes, Stendhal is turned based internally), it prevents doing non time critical stuff in another thread. In Stendhal we have the problem that any update or delete operation to the gameEvents table that takes more than a couple of seconds kills the server.
Database access in marauroa was designed for a single thread application. While this works most of the time in turn based games (yes, Stendhal is turn based internally), it prevents doing non time critical stuff in another thread. In Stendhal we have the problem that any update or delete operation to the gameEvents table that takes more than a couple of seconds, kills the server.


== Requirements ==
== Requirements ==
Line 17: Line 17:
== Design ==
== Design ==


[[Image:Marauroa.server.game.db.png]]
<!-- OUTDATED [[Image:Marauroa.server.game.db.png]] -->


== Concept FAQ ==
== Concept FAQ ==
Line 23: Line 23:
=== Where did IDatabase / JDBCDatabase go? ===
=== Where did IDatabase / JDBCDatabase go? ===


It was replaced by smaller classes specialized on one task each. They can be found in the package <code>marauroa.server.game.db</code> ending with "DAO".
It was replaced by smaller classes specializing in one task each. They can be found in the package <code>marauroa.server.game.db</code> and they end with "DAO".


=== What are those new ...DAO classes? How do they work? ===
=== What are those new ...DAO classes? How do they work? ===


They are "data access object". The basic idea is and has been from the start that the database related code is not in the original classes but at some central point outside the game logic. This used to be JDBCDatabase but one single class for all database operations is very unhandy. So JDBCDatabase has been splitted into a number of small classes focused on one area each: AccountDAO, CharacterDAO, GameEventDAO...
DAO stands for "data access object". The basic idea is, and has been from the start, that the database related code is not in the original classes but at some central point outside the game logic. This used to be JDBCDatabase, but one single class for all database operations is very hard to maintain. So JDBCDatabase has been split into a number of small classes focused on one area each: AccountDAO, CharacterDAO, GameEventDAO...


These classes replace the old IDatabase / JDBCDatabase and do the database stuff. All of their methods have two signatures: One with gets an DBTransaction object as first parameter and one without. This is for your convenience: In most cases it those functions are not part of a larger context so you do not have to care about transactions at all because the DAO-classes do the transaction handling on their own. There are, however, a small number of cases in which you want to do multiple calls to DAOs in one single transactions. In this case you get a DBTransaction from the TransactionPool and provide it as first parameter to DAO-methods. After you are done you must either commit or rollback your changes with the appropriate methods in the class TransactionPool.
These classes replace the old IDatabase / JDBCDatabase and do the database stuff. All of their methods have two signatures: One with a DBTransaction object as first parameter and one without. This is for your convenience: In most cases those functions are not part of a larger context so you do not have to care about transactions at all: the DAO-classes do the transaction handling on their own. There are, however, a small number of cases in which you want to do multiple calls to DAOs in one single transaction. In this case you get a DBTransaction from the TransactionPool and provide it as first parameter to DAO-methods. After you are done you must either commit or rollback your changes with the appropriate methods in the class TransactionPool.


=== I have extended the JDBCDatabase class. How does this work with DAOs? ===
=== I have extended the JDBCDatabase class. How does this work with DAOs? ===
Line 35: Line 35:
DAO classes should never be instantiated directly. Instead you should (and marauroa does) use the DAORegistry. This allows you to write a subclass of a DAO provided by marauroa and register it instead. If you are familiar with Spring, this is a similar concept. But without all the bulk of xml configuration files, parameter injection and interfaces with only one single implementation.
DAO classes should never be instantiated directly. Instead you should (and marauroa does) use the DAORegistry. This allows you to write a subclass of a DAO provided by marauroa and register it instead. If you are familiar with Spring, this is a similar concept. But without all the bulk of xml configuration files, parameter injection and interfaces with only one single implementation.


Imagine you want to subclass the CharacterDAO with your class SomeGameCharaterDAO:
Imagine you want to subclass the CharacterDAO with your class SomeGameCharacterDAO:
<source lang="java">
public class SomeGameCharacterDAO extends CharacterDAO {
public class SomeGameCharacterDAO extends CharacterDAO {
...
...
</source>

You simply register it as
You simply register it as
<source lang="java">
DAORegistry.get().register(CharacterDAO.class, new SomeGameCharaterDAO());
DAORegistry.get().register(CharacterDAO.class, new SomeGameCharacterDAO());
Note: In the register call the first parameter is the parent clase you want to replace.
</source>
Note: In the register call the first parameter is the parent class you want to replace.




=== What are those database adapters for? ===
=== What are those database adapters for? ===


The are a thin layer of database abstraction. This allows us to not only support MySQL but also database systems with don't require an external server.
They are a thin layer of database abstraction. This allows us to not only support MySQL but also database systems which don't require an external server.



=== Why is there a method getLastInsertId? ===
=== Why is there a method getLastInsertId? ===
Line 66: Line 68:
=== What happened to Transaction, JDBCTransaction, Accessor, JDBCAccess? ===
=== What happened to Transaction, JDBCTransaction, Accessor, JDBCAccess? ===


They where replaced with DBTransaction. You can get a DBTransaction from the TransactionPool. Make sure that you commit or rollback it after use.
They were replaced with DBTransaction. You can get a DBTransaction from the TransactionPool. Make sure that you commit or rollback it after use.


=== What happend to JDBCSQLHelper and StringChecker? ===
=== What happened to JDBCSQLHelper and StringChecker? ===


They have been moved to marauroa.server.db
They have been moved to marauroa.server.db
Line 79: Line 81:
== Porting of JMaPacman ==
== Porting of JMaPacman ==


The following diff shows all the changes that were required to port JMaPacman. I think it may help you to get a feeling on how to adjust your own code.
The following diff shows the complete list of changes that were required to port JMaPacman. I think it may help you to get a feeling on how to adjust your own code.
<!--

Don't use syntax highlighting here because it does not work well with the diff characters and bold formation
<source lang="java">
-->
public class MaPacmanRPRuleProcessor implements IRPRuleProcessor
public class MaPacmanRPRuleProcessor implements IRPRuleProcessor
{
{
Line 162: Line 167:
}
}
}
}


[[Category:Marauroa]]