• 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

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

 
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 51
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 789
Python C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic