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.
Love all, trust a few, do wrong to none.
Hemant Thard
Ranch Hand
Joined: Dec 23, 2008
Posts: 119
posted
0
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.
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
Joined: Dec 23, 2008
Posts: 119
posted
0
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
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
Joined: Dec 23, 2008
Posts: 119
posted
0
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.
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
Joined: Dec 23, 2008
Posts: 119
posted
0
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) ).
oh!!! this cache is really confusing me Hemant. Unable to understand it.
Hemant Thard
Ranch Hand
Joined: Dec 23, 2008
Posts: 119
posted
0
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.
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, 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.
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.