spring - Data Exchange and Transactions in Layered Architecture -


i have done quiet lot of reading on topic still have open questions. imagine following scenario.


[presentation layer]

you want develop application 2 access points:

  • web frontend (view + controller based)
  • service api

so totally makes sence keep business logic seperate these different presentation views reuse.

[data layer]

at other end of layered architecture have data layer:

  • domain / model objects represent data mapped orm framework
  • data access objects (dao) providing create, read, update, delete (crud) functionality

this layer accesing data. keep data access specific logic in layer can subsituted storage system.

[service layer]

this layer in-between data layer , presentation layer business logic happens.


on 1 hand not want thread language or framework specific, on other hand want know how can achieved central transaction handling (rollbacks, commits). let's assume use spring convenient framework transaction management.

1. best place handle transactions ???

obviously not part of data access objects since want access , change multiple objects during 1 transaction. therefore transaction handling must applied on service level suggested spring framework.

but assume business logic like:

  • a) request objects db
  • b) request remote info these objects
  • c) update status of objects in db

since operation b may take undefined length of time, not span transactions on operation since allocate valuable system resources. of business logic has seperated rest.

does mean service layer has seperated in 2 layers, 1 transactional , 1 not ???

2. how data gets modified , retrieved ???

in order present data presentation layer must aware of domain objects. using daos service layer grants access these objets presentation layer. see 2 problems approach.

a) let's assume hibernate used conveniet orm framework. dependencies loaded lazy, true other orm frameworks, too. view code trying access complex object may lazy load exception because transactional context ended service layer.

what right way handle kind of situation ???

b) controller commonly using framework magic apply changes made in web form directly model objects. again outside of transactional context, means service layer must provide functionality reattach model objects new transaction , save them.

is right way ???

looking forward answers...

i'm not sure architecture stack you've got, ones i've encountered web layer > service layer > data layer. is, web layer access data layer via service layer. or can go 'direct' service layer @ data layer.

in these types of structures, jta transactions (by when turned on) default configured either participate in existing transaction, or start own. in spring, looks @transactional annotated on service layer method (for example updatecustomer). if web layer has controller method updatecustomerrequest calls service updatecustomer , calls createauditlogevent, , of must done in 1 transaction, web layer starts transaction, service layer participates in it, , transaction finishes in web layer after createauditlogevent. if service layer's updatecustomer invoked directly, transactionmanager starts new transaction around updatecustomer method only.

so, using model service layer 'lowest level of access' data layer, , web layer reuses/access via service layer, transactions can shared across various layers.


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

php - MySQLi multi_query results for later use -