This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Whether to store the Shopping Cart items in ArrayList session variable or save them in database  RSS feed

 
Monica Shiralkar
Ranch Hand
Posts: 914
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure which category does this question belong to so am posting this in Java in General forum. I am implementing a shopping cart. Which of the below ways should I use to implement the shopping cart:


Simply save the shopping cart items in an arrayList and save this arrayList variable in the session.

Or

Save all this in database. User hibernate and a one to many mapping between cart and items?

thanks.
 
Rob Spoor
Sheriff
Posts: 21048
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends on whether or not you want the cart to be intact if someone comes back a day (or more) later. If so then the session will not be enough; that will usually expire within half an hour.
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition to Rob's:
These days I see some sites allow adding to cart even if user is not logged in.
So if you are building your application on the same line then you can implement both.

1. If user is logged in save the cart information in DB
2. If user is not logged in put the cart in session
 
Monica Shiralkar
Ranch Hand
Posts: 914
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks all

These days I see some sites allow adding to cart even if user is not logged in.
So if you are building your application on the same line then you can implement both.

1. If user is logged in save the cart information in DB
2. If user is not logged in put the cart in session


Thanks all. I have no login page, just that the user of application selects the products ,adds to the cart and enters his payment details during checkout. When he will click on add to cart the cart variable of current session will be retrieved. Is there any issue which this can cause?
 
Kaxhif Khan
Ranch Hand
Posts: 51
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There could be multiple options depending on the need of application. Accessing data in session is faster than accessing data from database. I would have implemented this functionality via the following steps

i) Store data in session to be used accross requests.
ii) Save data in database when use hit logout (it depends on whether you need to store cart information if user visit after 1 month etc ).

Hope this helps ..
 
Monica Shiralkar
Ranch Hand
Posts: 914
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There could be multiple options depending on the need of application. Accessing data in session is faster than accessing data from database. I would have implemented this functionality via the following steps

i) Store data in session to be used accross requests.
ii) Save data in database when use hit logout (it depends on whether you need to store cart information if user visit after 1 month etc ).

Hope this helps .


thanks.Is it ok do do this for the current session without the user login?
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a strategy. You want to give the user every encouragement and opportunity to buy. That means if he was logged in, then you would certainly add items to the database as they were put into the cart. You would also like to let him add stuff to a cart without being logged in, and you would like to make even that persist somehow, possibly on his computer, until such time as he logged in.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66142
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sessions are certainly not a General Java concept, so this has been moved to the Servlets forum.
 
Monica Shiralkar
Ranch Hand
Posts: 914
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a strategy. You want to give the user every encouragement and opportunity to buy. That means if he was logged in, then you would certainly add items to the database as they were put into the cart. You would also like to let him add stuff to a cart without being logged in, and you would like to make even that persist somehow, possibly on his computer, until such time as he logged in.


thanks. That is the difference between a simple shopping cart and a good sophisticated shopping cart. I am trying to first make a simple cart and then keep improving it to make it into a good shopping cart as you and others have suggested.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!