I have a Java application that receives orders from a remote server then logs into an account and executes the order (I.e, purchases product and ships to correct address).
I am storing the account details (such as username, password, funds remaining in account) in an object called "AccountDetails". I am also storing the purchase details (such as shipping address, first and last name, product ID and so on) in an object called "OrderDetails).
Throughout the execution of program both account and orders will be added to program on constant basis.
My question is - if I exit application and then launch the application I understand the program needs someway to instantiate all previous objects (such as all pending orders and account details).
What is the best way to do this?
I understand I would store "the state" of each object in a MYSQL database and then when I launch program connect to DB and instantiate object. However, I'm not sure exactly the best way to instantiate the object.. And do I have the general right approach here?
Thanks
You could serialise the object every time it is changed or if your application has a GUI serialise the object and save it in some file(MYSQL database in your case) when the user clicks save button.
Your constructor should look something like this
public foo(){
if (file has your object)
//code for deserializing
else
//create new object
}
Related
Just as a precurser to the question, I am moderately new to Java, so please bear with me.
I am right now attempting to create an account system, and it seems that automating the account ID process is more difficult than previously thought. I want to be able to have customized user IDs for each individual object reference, so it's easy to access, count how many users there are, edit values in the object, etc.
I basically want:
int accountsMade=1;
userAccount (A1) = new userAccount(other Input);
And then the next time a user registers an account, the object reference will be:
userAccount (A2) = new userAccount(Different input);
Is there any way to automate this system? It seems as if there is, but I can't figure it out.
What you'd want to do is create and keep reference to a HashMap object. These are key-value pairs, in which providing a key (in this case, the user ID provided by the user) allows you to get the value (in this case, the user account object) in a very quick and efficient fashion.
The actual signature of the HashMap you'd want to use would be Map.
Here's the current documentation on HashMap: http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html.
However, I think there's a pretty significant issue with the design you're building right now, and that's that you're constructing a new userAccount object each time you get input. The concept of an account suggests some sort of continuity - saved data, passwords, etc. Right? GMail doesn't make a new account for you every time you sign in.
So, you're going to need somewhere to keep that data, somehow. You could either use an external database, like SQL, or, if you're 100% sure that your use cases and userbase will never, ever, change, you could just hardcode account objects into your prototype and check user login attempts against the keys in the HashMap. Either way, I'm not sure it'll work for your purposes to be constructing a userAccount object upon user login.
I'm creating an application using Java RMI. In my case, more than 10 users are adding data to the database. My application has a class GenerateID and it's on server side. For every user's id generation process is going through this class. It has a method named generateID() I need to know, if more than one users are inserting data to the database at same time, What will be happening?
Thanks in Advance!
I am developing a Spring Application in which many users would access that application at a time, now the scenario is if one user hold the particular data from database the other user shouldn't access that particular data until it get release, like as Review in stack overflow, give some suggestion to solve this problem.
Create a DB objects cache in your app. When user 1 requests an object (eg by ID) you read it from db, place it in the cache, lock it and remember who locked it (there should be a lock and lockedByUser fields in the object) and return it to user 1. When user 2 requests the same object you block him until lock is released or throw an Exception. It's just a basic idea.
I would like to get some advice on designing a count based access control. For example I want to restrict the number of users that a customer can create in my system based on their account. So by default a customer can create 2 users but if the upgrade their account they get to create 5 users and so on.
There are a few more features that I need to restrict on a similar basis.
The application follows a generic model so every feature exposed has a backing table and we have a class which handles the CRUD operation on that table. Also the application runs on multiple nodes and has a distributed cache.
The approach that I am taking to implement this is as follows
- I have a new table which captures the functionality to control and the allowed limit (stored per customer).
- I intercept the create method for all tables and check if the table in question needs to have access control applied. If so I fetch the count of created entities and compare against the limit to decide if I should allow the creation or not.
- I am using the database to handle synchronization in case of concurrent requests. So after the create method is called I update the table using the following where clause
where ( count_column + 1 ) = #countInMemory#
. i.e. the update will succeed only if the value stored in the DB + 1 = value in memory. This will ensure that even if two threads attempt a create at the same time, only one of them will be able to successfully update. The thread that successfully updates wins and the other one is rolled back. This way I do not need to synchronize any code in the application.
I would like to know if there is any other / better way of doing this. My application runs on Oracle and MySQL DB.
Thanks for the help.
When you roll back, do you retry (after fetching the new user count) or do you fail? I recommend the former, assuming that the new fetched user count would permit another user.
I've dealt with a similar system recently, and a few things to consider: do you want CustomerA to be able to transfer their users to CustomerB? (This assumes that customers are not independent, for example in our system CustomerA might be an IT manager and CustomerB might be an accounting manager working for the same company, and when one of CustomerA's employees moves to accounting he wants this to be reflected by CustomerB's account.) What happens to a customer's users when the customer is deleted? (In our case another customer/manager would need to adopt them, or else they would be deleted.) How are you storing the customer's user limit - in a separate table (e.g. a customer has type "Level2," and the customer-type table says that "Level2" customers can create 5 users), or in the customer's row (which is more error prone, but would also allow a per-customer override on their max user count), or a combination (a customer has a type column that says they can have 5 users, and an override column that says they can have an additional 3 users)?
But that's beside the point. Your DB synchronization is fine.
I have a GWT application that has RPC service on its back end. I'm currently trying to implement users support and the only question that still remains is the way I should store session data.
I'm storing session id using
getThreadLocalRequest().getSession().setAttribute("sid", "randomSIDgoeshere");
So, the first question is more to Java servlets than to GWT. Does this code guarantee that next time I make a call like this:
getThreadLocalRequest().getSession().getAttribute("sid");
It would either be null (in case it gets called for the user that hasn't yet visited the piece of code where SID attribute is set) or it would exactly the same SID I've already save for that user. In other words, are these 2 pieces of code user-specific? (by user I mean single browser on a single computer)
The second question is about storing the mappings between SIDs and some extra data like user id. In case I have a code like this:
public class MyGwtServiceImpl extends RemoteServiceServlet implements MyGwtService {
// SID to User ID mappings
private final Map<String, String> sessions =
new HashMap<String, String>();
...
}
Is it guaranteed that sessions is always the same object for all requests and its data will remain "alive" unless the whole application is terminated? (Tomcat is stopped for instance) Is it normal approach or I should persist all these mappings on my DB?
First:
Yes, it does guarantee. The next time you call getThreadLocalRequest().getSession().getAttribute("sid"); on the next request, the attribute sid will stay there. But remember, that's the local request area, thus only requests from the same user (browser + ip) shall share the information. That means:
Fact:
User A connects with Firefox
You store a random value X with the ID Y
Case 1
User A connects with Firefox
You can retrieve the X
Case 2
User A connects with Google Chrome
You cannot retrieve the value X
Case 3
User B connects with Firefox
You cannot retrieve the value X
So yes, the session's content is user-specific. What exists in one session does not imply that will exist in other session.
Second:
No, it is not guaranteed. Although most of the times the same servelet isntance will be called, it is not guaranteed that will always exist. If you want to persist with attributes in your servelet, you must declare those attributes as Static, and by so, THAT static attribute won't be user-specific. Or you can store in the ServeletContext
I say this because different implementations (like Glassfish) can terminate instances if the servelet isn't being required for a long period of time (as far as I remember, I'm not sure of this (Glassfish terminating the instance)). But there is no documentation saying that it does guarantee that will be the same instance, so you cannot declare non-static attributes and share between diferent instances.