• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Extending Sun's/Oracle's interface - IOException

 
Ranch Hand
Posts: 30
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I've been thinking of how to deal with the IOException in my application and here are my thoughts.

I decided to implement a cache in the Data class. Additionally (encouraged by some people at this forum) I extended Sun's DB interafce and provided two more methods:
  • init() - will be called to initialize cache (MAP<Integer, Room>)
  • exit() - will be called when application(server or standalone mode) terminates and will save records stored in the cache back in the database file

  • All the CRUD methods operate on the cache only so there is no problem of the IOException. Thanks to this solution I do not breake the contract of the Sun's interface.

    I was looking for an information how people deal with that situation and found an interesting post here.

    Max Habibi wrote:If you interpret the requirements religiously, then maybe Sun is trying to give you a hint on the implementation they'd like to see here. That is, maybe they're telling you there shouldn't be any IO in this method. And if that's true, maybe they expect you to add the record logically. That is, maybe they expected you to cache the db in memory, and simply add/remove from that store: thus, there's no IO. Then again, maybe not. This interpretation has it's own consequences. For example, when are you supposed to reconcile the cache with the actual file? When you exit? maybe, but it sounds like a lot to intuit from a single method's signature


    Simple scenario:
  • an examiner calls create(record) and gets IllegalStateException with a message: DB not initilized, call init() first
  • so she/he looks through the documentation and sees that that there is an init() throws IOException declared in an interface DBUrlyBird extends DB
  • he also sees that Data implements DBUrlyBird is a singleton and the docs provide the information how to get an instace
  • while trying to call init() the compiler error is raised: IOException bla bla ...

  • What do you think of that solution ? Probably there is a lot of ways to resolve this issue but will this one be acceptable ?

    I didn't find anyone who used this approach.

    Thanks
    RK
     
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    When my init-method fails, because of an IOException I throw a (checked) DBException. And I passed, so nothing wrong with such approach
     
    Rafal Kawecki
    Ranch Hand
    Posts: 30
    Eclipse IDE Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    wow, great man

    this solution seems to be clear and nice. Thanks
     
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    There is a reason for throwing a generic DBException: your interface should hide implementation details. If you throw an IOException it seems you are using file I/O. And what if you have to create an implementation of your interface for a RDBMS, like MySQL. You probably have to deal with SQLExceptions (instead of IOExceptions). Using this DBException solves this issue in a clean nice way.
     
    Rafal Kawecki
    Ranch Hand
    Posts: 30
    Eclipse IDE Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    That's right. The scenario I provided was just an example. I was rather thinking of checked exceptions (not specifically of IOException). Anyway, thanks for pointing this out. That's is a very useful infomration for all of us.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic