Refactoring Database Access in Marauroa: Difference between revisions
Content deleted Content added
imported>Hendrik Brummermann |
imported>Kymara |
||
| (90 intermediate revisions by 3 users not shown) | |||
Line 1:
== Requirements ==
Line 17:
== Design ==
<!-- OUTDATED [[Image:Marauroa.server.game.db.png]] -->
== Concept FAQ ==
Line 23:
=== Where did IDatabase / JDBCDatabase go? ===
It was replaced by smaller classes
=== What are those new ...DAO classes? How do they work? ===
These classes replace the old IDatabase / JDBCDatabase and do the database stuff. All of their methods have two signatures: One with
=== I have extended the JDBCDatabase class. How does this work with DAOs? ===
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.
Imagine you want to subclass the CharacterDAO with your class
<source lang="java">
public class SomeGameCharacterDAO extends CharacterDAO {
...
</source>
You simply register it as
<source lang="java">
DAORegistry.get().register(CharacterDAO.class, new SomeGameCharacterDAO());
</source>
Note: In the register call the first parameter is the parent class you want to replace.
=== What are those database adapters for? ===
=== Why is there a method getLastInsertId? ===
Line 66 ⟶ 68:
=== What happened to Transaction, JDBCTransaction, Accessor, JDBCAccess? ===
They
=== What
They have been moved to marauroa.server.db
Line 75 ⟶ 77:
Please have a look at the ...DAO classes in the marauroa.server.game.db package and split your sub class accordingly. After that is done you need to register them in the DAORegistry with the class of the original DAO and an instance of your class.
== Porting of JMaPacman ==
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 AccountResult createAccount(String username, String password, String email) {
- IDatabase database = DatabaseFactory.getDatabase();
+ '''TransactionPool transactionPool = TransactionPool.get();'''
- Transaction trans = database.getTransaction();
+ '''DBTransaction trans = transactionPool.beginWork();'''
+ '''AccountDAO accountDAO = DAORegister.get().get(AccountDAO.class);'''
try {
- trans.begin();
- if (database.hasPlayer(trans, username)) {
+ if ('''accountDAO'''.hasPlayer(trans, username)) {
return new AccountResult(Result.FAILED_PLAYER_EXISTS, username);
}
- database.addPlayer(trans, username, Hash.hash(password), email);
+ '''accountDAO'''.addPlayer(trans, username, Hash.hash(password), email);
- trans.commit();
+ '''transactionPool.commit(trans);'''
return new AccountResult(Result.OK_CREATED, username);
} catch (SQLException e1) {
- try {
- trans.rollback();
- } catch (SQLException e2) {
- //logger.error("Rollback failed: ", e2);
- System.out.println("Rollback failed: " + e2);
- }
+ '''transactionPool.rollback(trans);'''
return new AccountResult(Result.FAILED_EXCEPTION, username);
}
}
public CharacterResult createCharacter(String username, String character, RPObject tmpl) {
- IDatabase database = DatabaseFactory.getDatabase();
+ '''TransactionPool transactionPool = TransactionPool.get();'''
- Transaction trans = database.getTransaction();
+ '''DBTransaction trans = transactionPool.beginWork();'''
+ CharacterDAO characterDAO = DAORegister.get().get(CharacterDAO.class);
try {
- trans.begin();
- if (database.hasCharacter(trans, username, character)) {
+ if ('''characterDAO'''.hasCharacter(trans, username, character)) {
return new CharacterResult(Result.FAILED_PLAYER_EXISTS, character, tmpl);
}
@@ -196,18 +190,13 @@
- database.addCharacter(trans, username, character, object);
+ '''characterDAO'''.addCharacter(trans, username, character, object);
- trans.commit();
+ '''transactionPool.commit(trans);'''
return new CharacterResult(Result.OK_CREATED, character, object);
} catch (Exception e1) {
//logger.warn("SQL exception while trying to create a new character: ", e1);
- try {
- trans.rollback();
- } catch (SQLException e2) {
- //logger.error("Rollback failed: ", e2);
- System.out.println("Rollback failed: " + e2);
- }
+ '''transactionPool.rollback(trans);'''
return new CharacterResult(Result.FAILED_EXCEPTION, character, tmpl);
}
}
[[Category:Marauroa]]
| |||