Home » Eclipse Projects » Eclipse Scout » Multiple databases
| |
Re: Multiple databases [message #902322 is a reply to message #902298] |
Fri, 17 August 2012 05:00 |
Jeremie Bresson Messages: 124 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 05:06] Report message to a moderator
|
|
| | | |
Re: Multiple databases [message #950929 is a reply to message #902538] |
Sat, 20 October 2012 08:30 |
|
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 297 times)
[Updated on: Sat, 20 October 2012 08:33] Report message to a moderator
|
|
|
Re: Multiple databases [message #954059 is a reply to message #950929] |
Mon, 22 October 2012 19:07 |
Jeremie Bresson Messages: 124 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.
|
|
| |
Re: Multiple databases [message #1234554 is a reply to message #1234389] |
Wed, 22 January 2014 08:53 |
Jeremie Bresson Messages: 1252 Registered: October 2011 |
Senior Member |
|
|
For you lookup call, I assume you use a AbstractSqlLookupService.
As you see in the source code, this class uses
SQL.selectLimited(preprocessedSql, call.getMaxRowCount(), call);
in execLoadLookupRows.
You can override this method and control the SqlService you want.
---
SQL is a convenience class that delegates all the call to the default SQL service (implementation of org.eclipse.scout.rt.server.services.common.jdbc.ISqlService)
SQL.select(..) is the same as SERVICES.getService(<default SQL service>).select(..)
The default SQL service is defined by the priority ranking of the service.
There is different way to set the priority:
* In plugin.xml (read org.eclipse.scout.service.services service vs. proxy to know the default value)
* @Priority annotation on top of your implementation of the service (MySqlService.java)
---
I hope it helps.
[Updated on: Wed, 22 January 2014 08:54] Report message to a moderator
|
|
| | |
Goto Forum:
Current Time: Fri Apr 26 13:46:56 GMT 2024
Powered by FUDForum. Page generated in 0.04164 seconds
|