Home » Eclipse Projects » scout » Multiple databases
| Multiple databases [message #902247] |
Thu, 16 August 2012 10:06  |
Bertin Kiekebosch Messages: 227 Registered: August 2011 |
Senior Member |
|
|
Hi,
until now I always created an application that worked against one database. I now have a requirement that needs to pull in data from another database. I did a little test and created a second SQL service.
It seems that this works OK, at least for the queries I did. Although before I proceed with this, has anybody experience with this, and can anybody confirm that working with multiple databases works well, included transaction handling, roll back etc.
Regards Bertin
|
|
| |
| Re: Multiple databases [message #902322 is a reply to message #902298] |
Fri, 17 August 2012 01:00   |
Jeremie Bresson Messages: 108 Registered: November 2010 |
Senior Member |
|
|
Hi Bertin,
If I understood your question correctly, you want to use 2 Sql Services at the same time. This is not a problem at all.
With the SDK you create the services (server > Common Services > Sql Services):
* FirstSqlService
* SecondSqlService
You configure them with the getConfigured*(..) methods or with the config.ini properties.
When you use it, instead of using the SQL convenience class, you can use SERVICES and a direct reference to the service class:
Object[][] select1 = SERVICES.getService(FirstSqlService.class).select("select * from questions");
System.out.println(select1.length);
Object[][] select2 = SERVICES.getService(SecondSqlService.class).select("select * from actors");
System.out.println(select2.length);
Sidenote:
The SQL convenience class calls SERVICES.getService(ISqlService.class). If you want to ensure which implementation will be returned in this case (FirstSqlService or SecondSqlService), you can use the @Priority annotation to make one of your service primary and the other secondary.
[Updated on: Fri, 17 August 2012 01:06] Report message to a moderator
|
|
| | | |
| Re: Multiple databases [message #950929 is a reply to message #902538] |
Sat, 20 October 2012 04:30   |
 |
Stathis Alexopoulos Messages: 40 Registered: September 2010 |
Member |
|
|
Hi to all,
I have the same requirement with Bertin, and because i am still getting knowing the Scout, i still using the convenient SQL class.
I think that the best solution would be to subclassing the SQL to use another ISqlService. Ufortunatelly the SQL class is final and uses everywhere the
ISqlService service = SERVICES.getService(usedServiceType);
I would recommend to rewrite it, but who am i to recommend. Anyway, i wrote an SQL2 class which is copy/paste of SQL, with four differences.
- It is not final.
- It has a protected constructor.
- It defines a getSqlService(), which can be extended.
- Every underlined method uses the getSqlService().
With that implementation someone could extend the SQL2 as following
class YourSql extends Sql2 {
private YourSql() {
}
public ISqlService getSqlService() {
return SERVICES.getService(YourSqlService.class);
}
}
and now can use the following syntax.
For anyone interesting i am uploading the code of SQL2. I would appreciate it if i had comments about other ways to implement it.
Attachment: SQL2.java
(Size: 5.40KB, Downloaded 41 times)
[Updated on: Sat, 20 October 2012 04:33] Report message to a moderator
|
|
|
| Re: Multiple databases [message #954059 is a reply to message #950929] |
Mon, 22 October 2012 15:07  |
Jeremie Bresson Messages: 108 Registered: November 2010 |
Senior Member |
|
|
To my mind, your code will not work.
SQL2.getSqlService() is a static method (public static), so you can not override this method in the child class (YourSql, the class that is extending SQL2).
SQL is a convenience class... It is suitable for one SQL Service (the default SQL Service. The one that will be returned by "SERVICES.getService(ISqlService.class)").
Do not expect to do more with this class. It is final, because it is not expected that someone create a subclass. If you have 2 services, you need to handle them separately.
ISqlService mainDb = SERVICES.getService(FirstSqlService.class);
mainDb.selectInto("select question_text from questions where question_id = :questionId into :questionText", formData);
ISqlService archiveDb = SERVICES.getService(SecondSqlService.class);
archiveDb.insert("insert facts(question_id, question_text, f1, f2, f3) values (:questionId, :questionText, :fact1, :fact2, :fact3", formData);
If you want convenience classes for your 2 SQL service, I recommend to write a class that will return the correct service:
public final class MainDb {
public static ISqlService get() {
return SERVICES.getService(FirstSqlService.class);
}
}
and:
public final class ArchiveDb {
public static ISqlService get() {
return SERVICES.getService(SecondSqlService.class);
}
}
In your code, it looks like you are applying the singleton pattern [with get() instead of getInstance()]
And you can use it:
MainDb.get().selectInto("select question_text from questions where question_id = :questionId into :questionText", formData);
ExternalDb.get().insert("insert facts(question_id, question_text, f1, f2, f3) values (:questionId, :questionText, :fact1, :fact2, :fact3", formData);
You also can also combine the 2 service accessors in one class:
public final class SQL2 {
public static ISqlService main() {
return SERVICES.getService(FirstSqlService.class);
}
public static ISqlService archive() {
return SERVICES.getService(SecondSqlService.class);
}
}
Your code will look like this:
SQL2.main().selectInto("select question_text from questions where question_id = :questionId into :questionText", formData);
SQL2.archive().insert("insert facts(question_id, question_text, f1, f2, f3) values (:questionId, :questionText, :fact1, :fact2, :fact3", formData);
I hope it helps.
|
|
|
Goto Forum:
Current Time: Wed Jun 19 22:15:30 EDT 2013
Powered by FUDForum. Page generated in 0.01653 seconds
|