Reputation: 652
I know that there's lot of documentation about, but i'm totally newbie on this argument and I would like to have some simple advices about that. I'm working with spring hibernate and jpa. I'm going to have two different applications on the same server. Both of them communicate with the same database, but one just read and the other read and write. Which is the best way to afford this? Looking aorund i understand that should be necessary a second-level cache like EHCache, but there are some stupid things that i can't understand. For example, should I create exactly the same entities in both applications? How can read-only application associate its entities with the ones created by the other application?
Upvotes: 3
Views: 3032
Reputation: 15229
Well, here's some remarks on your question
Theoretically, you're not at all forced to have the same Java Classes mapped to the database tables. You could make different ones and annotate them such that both sets map to the same tables. That being said, this is not too good a practice. What you should do is have a data-access-module where you have your set of JPA Entity beans mapped to the tables, as well as the classes that allow you to do basic operations on these (at least CRUD), then have separate service classes in each of your two projects that use the data-access-module classes to implement the specific business logic
Since you're having 2 applications accessing the same database, you'll be dealing with two separate JPA Contexts, which will flush and synchronize separately. Because of this, some data inconsistencies may appear, if you don't make sure to manually flush the modifications each time you need to. This could be further an issue if you use a 2nd level cache on top of it all (like EHCache). However, in your current setup, only the application that reads AND writes should adhere to these restrictions, the other one (the one that's read only) can, and indeed it is advised, to use a second level cache to speed up its operations. Just make sure that said cache is refreshed/expired such that you'll get the desired data when it's written by the first app (i.e., if you makes writes often, your cache should also expire often. If you do writes once a month you can configure a longer expire time on the read-only application's 2nd level cache)
Upvotes: 6
Reputation: 32437
I would just create two users, one of which has only read access (if that's supported by your DB engine).
Upvotes: 0