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

Can I use Hibernate in my RFID based attendance tracking application?

 
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi all, I am working on a project, its in the designing level. I just want to know one thing whether hibernate can be used in my application on or. Because session object is not thread safe, I read.

The description goes like this:

This application is meant to track the attendance details of a students and staff in a school. Each and every student and staff are given with a swipe card containing their ID. A huge hardware is deployed in the school's LAN which consists of the following:

1. RFID Devices
2. RFID switches. (RFID devices are connected to this switches, 2 RFIDs/switch)
3. Network/ethernet switch (All RFID switches are plugged to the ethernet switch)

Each and every RFID switch has got its own IP address and implements FTP protocol. All these switches act like server sockets.

I have to write a java client program for each and every RFID switch and listen to it. For each and every RFID switch a will start a new client thread.

The number of RFID switches can vary at runtime. So I will use a for loop for each and every client thread provided the ip addresses of each and every switch before starting the application.

Whenever a person swipes his card at a terminal(RFID) the appropriate client socket reads the data and inserts into the database.

Note: I have already tested the RFID switch whether am I able to read values from it or not.

My doubt is can I use hibernate in such kind of environment? Can anyone please help me?

Thank you all in advance.
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
hi chaitanya,

I am not an expert on multi threading .

but as per my understanding, your requirement is analogue to how our application server works.
that is for each client request, container create a separate thread for process it.

and where did you read this
"Because session object is not thread safe, I read. "

can you quote the source, because as per my understanding
"hibernate session is a single thread, non-shared object " and so thread safe.


Regards,
Hemant

 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi Hemant, I read it in "Manning - Hibernate in action" book. "Section 2.2.1 The core interfaces" says that.

Session interface
The Session interface is the primary interface used by Hibernate applications. An
instance of Session is lightweight and is inexpensive to create and destroy. This is
important because your application will need to create and destroy sessions all the
time, perhaps on every request. Hibernate sessions are not threadsafe and should
by design be used by only one thread at a time.

Suppose that I have 4 client socket threads. Someone swiped at terminal 1, client socket 1 reads the data and tries to insert a record in database, at the same time if another person swipes at other terminal say 2 or 3 or 4 the appropriate client socket will receive the data and tries to insert a record in database.

Will anything happen if I use hibernate in my application?
 
Hemant Thard
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi chaitanya,

Thanks for correcting me up .

but still i don't see why the hibernate can't be used.
say client 1,socket 1, calls your client code, creates session for reading and writing data.
same for other request, it will have there own hibernate session for reading and writing data.

so the session that you will be creating is local to your thread and there wont be any session thread safe issue.



this is how i see your application will be implement hibernate session.
but i am not sure though, because of your use of term "session object is not thread safe".
let me know if how are you planning to implement it in your application

Hope this helps,
Hemant
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Thanks Hemant, I want to use hibernate in my application. I ll attach a sample image ASAP
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
This is the sample diagram of my application.
111.PNG
[Thumbnail for 111.PNG]
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi Hemant, let me tell you what I am thinking about hibernate in my application.

Suppose that a student XYZ swiped his card at terminal1, thread1 will read the data. If XYZ is not inside the the intime will be inserted into database. Now if XYZ swiped his card at terminal2, thread2 will read the data then check whether he is in or not. XYZ is in, so the outtime will be noted. Thread1 will have its own session and thread2 will have its own session. session1 and session2 has their own cache.

Now suppose XYZ swipes at terminal1, thread1 will read and hand over the data to session1. Since the outtime in session1 is null, the outtime will be noted. Whereas this time XYZ is inside the school.

This is what I am thinking will happen. Is my guess true?
 
Hemant Thard
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
HI



Now suppose XYZ swipes at terminal1, thread1 will read and hand over the data to session1. Since the outtime in session1 is null, the outtime will be noted. Whereas this time XYZ is inside the school.



here don't you think a new thread will be created which will have a new session.

Regards,
Hemant
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi Hemant, I will try to describe more detailed.

Suppose from terminal1--thread1 a person entered in, a record is created like "1(transid) 1234(pid) 01-01-2011 11:30:23(timein) null(timeout)". Here session1 will save the data. Suppose that this record in session1's cache.

Now from terminal2--thread2 the same person exits, the record is updated like "1(transid) 1234(pid) 01-01-2011 11:30:23(timein) 01-01-2011 13:30:24(timeout)". Here session2 will save the data. Suppose that this record is also in session2's cache.

Now from terminal1--thread1 the same person entered in, this time the session will check in its cache. Since the person already entered, session1 tries to update his timeout. Where as the person is again entering, not leaving.

I am thinking this problem might occur. This is just my imagination. I am not sure of this.
 
Hemant Thard
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
hi chaitanya,

you are right.
if you are having only one session per terminal, then this problem might occur.
only way out to this problem is to flush (session.flush() )your session and then clear that object from cache(session.evict(Object object) ).

Hope this helps,
Hemant

 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
What if I use, only 1 session for all threads? Will it be a good approach?
 
Hemant Thard
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
hi ,

no additional benefit.
even if you use only a single session, still you will have to use this approach(flush and evict )

for the objects that are common(and not changing frequently), you always have to option to implement second level cache.



 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Thanks Hemant, I will definitely implement what you said.

One doubt Hemant, What about the same implementation in a web application. Since everything is multithreaded over there?
 
Hemant Thard
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi,

when we talk about multi-threading,each thread has its own session copy.
new Session object is created when you call sessionfactory.getsession();


 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I guess there also we have to flush the session and evcit it? Am I right?
 
Hemant Thard
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
hi,

no.
when i say,each thread has its own session copy, it means that session has scope defined with in its boundary.

for example


here the scope of the hibernate session is that of the method.

now even if multiple thread is accessing this the method, each thread will have its own session object, its own Tx and its own cache.


Hope i am clear.

Regards,
Hemant.
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
oh!!! this cache is really confusing me Hemant. Unable to understand it.
 
Hemant Thard
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
hi,

well to brief you,there are 3 type of cache in hibernate.

persistence-context: also known as first level cache, which starts when you open the session (sf.openSession()) and closes when you close the session(session.close()).
that's the scope of first level cache. outside this boundary, this cache doesnt exists.

process cache : second level cache, has scope of entire process(session factory ), has to be implement manually by using any of cache provider, like ehcache, jbosscache etc.

cluster : second level cache, has scope of entire cluster(multiple server), has to be implement manually.

 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi Hemant, let me tell what is in my mind about hibernate cache.

There are two caches, primary and secondary. I dont know anything about secondary cache. What will be in that and when objects are cached in that. Let keep this second level cache aside.

All I know is about first level cache. Session is the first level cache. Whenever I select something from database it will be cached in first level cache. If I ask for same records for next time an object is returned from cache, not from database. If I use Session.clear() all objects cached will be erased. Again if I select something from database, I ll get data directly from database not from cache this time.

This is what i understood till now about first level cache.

Now in a multi threaded environment such as web application, suppose a user makes a request to resource1, session1 associated with resource1 will get the details. Meanwhile if someone changes the data using resource2, session2, what about the data in session1. It wont be updated.

I hope you understood my doubt and what I understood about primary cache.
 
Hemant Thard
Ranch Hand
Posts: 122
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
hi,

here is a article that will clear your doubt.
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hemant, could you please refer me a text book which is easy to understand, such as "Head first servlets and jsps" book. The language used is very easy in that book. Could you please refer such kind of text book.
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Thanks Hemant.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi chaitanya karthikk,

I also work with same type of project can you help me please. I am in problem how to access the data from RFID device by java program.

 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Kundan,

This topic is over a year old and it is unlikely the original posters are still following it. Please choose what you feel is the most appropriate forum and start a new topic with your question.
 
    Bookmark Topic Watch Topic
  • New Topic