473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Singleton

Hi,
I am working on an ASP.NET application where I need to create a unique
key in a page. When a customer clicks on the button "Generate Key" I
need to create a key which should be unique in the database. I am
plannig to create in the format MMDDYYHHMMSS.

I have written a class that generates the key:

public class GenUniqueID
{
private static readonly GenUniqueID instUnique=new GenUniqueID();

private StringBuilder strUnique=new StringBuilder("");

private GenUniqueID()
{
DateTime dtID=DateTime.Now;
strUnique.Append(dtID.Month);
strUnique.Append(dtID.Day);
strUnique.Append((dtID.Year.ToString()).Substring( 2,2));
strUnique.Append(dtID.Hour);
strUnique.Append(dtID.Minute);
strUnique.Append(dtID.Second);
}

public static GenUniqueID Instance
{
return instUnique;
}

public static string UniqueID
{
get
{
return strUnique.ToString();
}
}
}

I'll call this method from my aspx page :

GenUniqueID key=GenUniqueID.Instance();
string strKey=key.UniqueID;

Will this guarantee a UNIQUE key even if two or more users from
different locations will click on "Generate Key" at the same time???

Thanks,
Ashish.

Nov 15 '06 #1
12 2055
No - it won't; static fields in ASP.NET should only hold application-wide
data, and never anything session / page related (unless neatly divided such
as in a sync'd cache keyed by user). Otherwise you are just asking for a
thread race, or (in this case) for lots of users to share an id.

There are other solutions for making this available to other bits of code
(such as state), but personally I would happily pass around an object
containing this information, rather than rely on these back doors - it also
makes it easier to re-use the code outside of asp.net.

Marc
Nov 15 '06 #2
No, if they press the button at the same time, or more importantly, the
request hits the server at the same time, they will receive the same
ID. You could investigate GUIDs which would be simpler.
as****@gmail.com wrote:
Hi,
I am working on an ASP.NET application where I need to create a unique
key in a page. When a customer clicks on the button "Generate Key" I
need to create a key which should be unique in the database. I am
plannig to create in the format MMDDYYHHMMSS.

I have written a class that generates the key:

public class GenUniqueID
{
private static readonly GenUniqueID instUnique=new GenUniqueID();

private StringBuilder strUnique=new StringBuilder("");

private GenUniqueID()
{
DateTime dtID=DateTime.Now;
strUnique.Append(dtID.Month);
strUnique.Append(dtID.Day);
strUnique.Append((dtID.Year.ToString()).Substring( 2,2));
strUnique.Append(dtID.Hour);
strUnique.Append(dtID.Minute);
strUnique.Append(dtID.Second);
}

public static GenUniqueID Instance
{
return instUnique;
}

public static string UniqueID
{
get
{
return strUnique.ToString();
}
}
}

I'll call this method from my aspx page :

GenUniqueID key=GenUniqueID.Instance();
string strKey=key.UniqueID;

Will this guarantee a UNIQUE key even if two or more users from
different locations will click on "Generate Key" at the same time???

Thanks,
Ashish.
Nov 15 '06 #3
One second is not enough to get unique key, because several users can get the
same key in one second.
I'd recomed to use either GUID for this, or add user's specific info (like
name) to your unique key

--
WBR,
Michael Nemtsev :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche


"as****@gmail.com" wrote:
Hi,
I am working on an ASP.NET application where I need to create a unique
key in a page. When a customer clicks on the button "Generate Key" I
need to create a key which should be unique in the database. I am
plannig to create in the format MMDDYYHHMMSS.

I have written a class that generates the key:

public class GenUniqueID
{
private static readonly GenUniqueID instUnique=new GenUniqueID();

private StringBuilder strUnique=new StringBuilder("");

private GenUniqueID()
{
DateTime dtID=DateTime.Now;
strUnique.Append(dtID.Month);
strUnique.Append(dtID.Day);
strUnique.Append((dtID.Year.ToString()).Substring( 2,2));
strUnique.Append(dtID.Hour);
strUnique.Append(dtID.Minute);
strUnique.Append(dtID.Second);
}

public static GenUniqueID Instance
{
return instUnique;
}

public static string UniqueID
{
get
{
return strUnique.ToString();
}
}
}

I'll call this method from my aspx page :

GenUniqueID key=GenUniqueID.Instance();
string strKey=key.UniqueID;

Will this guarantee a UNIQUE key even if two or more users from
different locations will click on "Generate Key" at the same time???

Thanks,
Ashish.

Nov 15 '06 #4
I do not want to use GUID because the key should be in a readable
format (like MMDDYYHHMMSS). Is there any other way I can generate this
uniquely?

DeveloperX wrote:
No, if they press the button at the same time, or more importantly, the
request hits the server at the same time, they will receive the same
ID. You could investigate GUIDs which would be simpler.
as****@gmail.com wrote:
Hi,
I am working on an ASP.NET application where I need to create a unique
key in a page. When a customer clicks on the button "Generate Key" I
need to create a key which should be unique in the database. I am
plannig to create in the format MMDDYYHHMMSS.

I have written a class that generates the key:

public class GenUniqueID
{
private static readonly GenUniqueID instUnique=new GenUniqueID();

private StringBuilder strUnique=new StringBuilder("");

private GenUniqueID()
{
DateTime dtID=DateTime.Now;
strUnique.Append(dtID.Month);
strUnique.Append(dtID.Day);
strUnique.Append((dtID.Year.ToString()).Substring( 2,2));
strUnique.Append(dtID.Hour);
strUnique.Append(dtID.Minute);
strUnique.Append(dtID.Second);
}

public static GenUniqueID Instance
{
return instUnique;
}

public static string UniqueID
{
get
{
return strUnique.ToString();
}
}
}

I'll call this method from my aspx page :

GenUniqueID key=GenUniqueID.Instance();
string strKey=key.UniqueID;

Will this guarantee a UNIQUE key even if two or more users from
different locations will click on "Generate Key" at the same time???

Thanks,
Ashish.
Nov 15 '06 #5
Why not just use Guid.NewGuid()? That's what Guids are for. You can store
them in the database as type UNIQUEIDENTIFIER.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"as****@gmail.com" wrote:
Hi,
I am working on an ASP.NET application where I need to create a unique
key in a page. When a customer clicks on the button "Generate Key" I
need to create a key which should be unique in the database. I am
plannig to create in the format MMDDYYHHMMSS.

I have written a class that generates the key:

public class GenUniqueID
{
private static readonly GenUniqueID instUnique=new GenUniqueID();

private StringBuilder strUnique=new StringBuilder("");

private GenUniqueID()
{
DateTime dtID=DateTime.Now;
strUnique.Append(dtID.Month);
strUnique.Append(dtID.Day);
strUnique.Append((dtID.Year.ToString()).Substring( 2,2));
strUnique.Append(dtID.Hour);
strUnique.Append(dtID.Minute);
strUnique.Append(dtID.Second);
}

public static GenUniqueID Instance
{
return instUnique;
}

public static string UniqueID
{
get
{
return strUnique.ToString();
}
}
}

I'll call this method from my aspx page :

GenUniqueID key=GenUniqueID.Instance();
string strKey=key.UniqueID;

Will this guarantee a UNIQUE key even if two or more users from
different locations will click on "Generate Key" at the same time???

Thanks,
Ashish.

Nov 15 '06 #6
I am sorry but I could not understand the solution you suggested.
can you please show me a sample code ?
- Ashish
Marc Gravell wrote:
No - it won't; static fields in ASP.NET should only hold application-wide
data, and never anything session / page related (unless neatly divided such
as in a sync'd cache keyed by user). Otherwise you are just asking for a
thread race, or (in this case) for lots of users to share an id.

There are other solutions for making this available to other bits of code
(such as state), but personally I would happily pass around an object
containing this information, rather than rely on these back doors - it also
makes it easier to re-use the code outside of asp.net.

Marc
Nov 15 '06 #7
Add to your key User's details, hash of client's browser/system info,
client's IP, and some rand number
It should be enough

--
WBR,
Michael Nemtsev :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche


"as****@gmail.com" wrote:
I do not want to use GUID because the key should be in a readable
format (like MMDDYYHHMMSS). Is there any other way I can generate this
uniquely?

DeveloperX wrote:
No, if they press the button at the same time, or more importantly, the
request hits the server at the same time, they will receive the same
ID. You could investigate GUIDs which would be simpler.
as****@gmail.com wrote:
Hi,
I am working on an ASP.NET application where I need to create a unique
key in a page. When a customer clicks on the button "Generate Key" I
need to create a key which should be unique in the database. I am
plannig to create in the format MMDDYYHHMMSS.
>
I have written a class that generates the key:
>
public class GenUniqueID
{
private static readonly GenUniqueID instUnique=new GenUniqueID();
>
private StringBuilder strUnique=new StringBuilder("");
>
private GenUniqueID()
{
DateTime dtID=DateTime.Now;
strUnique.Append(dtID.Month);
strUnique.Append(dtID.Day);
strUnique.Append((dtID.Year.ToString()).Substring( 2,2));
strUnique.Append(dtID.Hour);
strUnique.Append(dtID.Minute);
strUnique.Append(dtID.Second);
}
>
public static GenUniqueID Instance
{
return instUnique;
}
>
public static string UniqueID
{
get
{
return strUnique.ToString();
}
}
}
>
I'll call this method from my aspx page :
>
GenUniqueID key=GenUniqueID.Instance();
string strKey=key.UniqueID;
>
Will this guarantee a UNIQUE key even if two or more users from
different locations will click on "Generate Key" at the same time???
>
Thanks,
Ashish.

Nov 15 '06 #8
Well, I was mainly just observing that your use of static is not safe here;
unless I am mis-reading your code, it will keep issuing the same key (from
the first time it is used) over-and-over-and-over.

Other people have observed that your key format is also not safe (multiple
hits in a second).

Also note that your code doesn't actually follow this format, as at 1am it
will (if I read correctly) insert only "1", not "01".

So basically, you have a job on!

I would simply have an instance class (no statics) which generates a key,
and I would probably delegate key generation to the database. Otherwise you
cannot possibly compensate for different servers in a cluster (not sure if
this is an issue for you; I suspect not). But a guid should work...

I think you really need to revisit what you are trying to do, and firgure
out a scheme that allows for lots of hits per second, doesn't allow simple
guessing (i.e. incrementing second-by-second through a single day could
probably guess an ID), etc...

Marc
Nov 15 '06 #9
as****@gmail.com wrote:
I do not want to use GUID because the key should be in a readable
format (like MMDDYYHHMMSS). Is there any other way I can generate this
uniquely?
The string representation of GUID is just as readable as MMDDYYHHMMSS. If
you are trying to use the key for dual purposes to be both a unique value
and an indicator as to when it was created, then I suggest you redesign this
to have the unique key be the unique key and use a separate value to hold a
timestamp as to when the record was generated.
--
Tom Porterfield

Nov 15 '06 #10
Yes, it returns the same key everytime!
I think I would go with delegating the key generation to the database.
Thanks for your suggestion.

But I am just wondering if it is possible to lock a method or prevent
creating another instance of the class while it is generating the key
for one request. The second request will be in a queue during this
time.
Hope it is not sounding crazy!

-Ashish.

Marc Gravell wrote:
Well, I was mainly just observing that your use of static is not safe here;
unless I am mis-reading your code, it will keep issuing the same key (from
the first time it is used) over-and-over-and-over.

Other people have observed that your key format is also not safe (multiple
hits in a second).

Also note that your code doesn't actually follow this format, as at 1am it
will (if I read correctly) insert only "1", not "01".

So basically, you have a job on!

I would simply have an instance class (no statics) which generates a key,
and I would probably delegate key generation to the database. Otherwise you
cannot possibly compensate for different servers in a cluster (not sure if
this is an issue for you; I suspect not). But a guid should work...

I think you really need to revisit what you are trying to do, and firgure
out a scheme that allows for lots of hits per second, doesn't allow simple
guessing (i.e. incrementing second-by-second through a single day could
probably guess an ID), etc...

Marc
Nov 15 '06 #11
Well, yes you could do this... but you will still have to throttle this to
one a second to meet your format limitations. Of course, without the format
you have chosen (i.e. with an incrementing ID from the database, or a guid)
you don't even have to do this locking : you can unleash all threads to run
full throttle. Which would be good.

Marc
Nov 15 '06 #12

Technically, you could solve the problem by:

using the lock {} statement, and Thread.Sleep (ing) for 1 second.

That's techincally, but it would be silly to do that.

http://www.informit.com/discussion/i...87464628c&rl=1
Look at a COMB, its a friendlier GUID I believe, though I've never done
this.

...

I know you said you don't want a Guid, but that's the best thing to use.
You could also look at using a TimeSpan object, and you can get down to the
MilliSecond there.
But you'd still need to lock it for a millisecond, just in case.
The world is going to GUID's . ... get on board ! They're not so bad after
you get used to a database with a bunch of GUID's in it.


<as****@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Hi,
I am working on an ASP.NET application where I need to create a unique
key in a page. When a customer clicks on the button "Generate Key" I
need to create a key which should be unique in the database. I am
plannig to create in the format MMDDYYHHMMSS.

I have written a class that generates the key:

public class GenUniqueID
{
private static readonly GenUniqueID instUnique=new GenUniqueID();

private StringBuilder strUnique=new StringBuilder("");

private GenUniqueID()
{
DateTime dtID=DateTime.Now;
strUnique.Append(dtID.Month);
strUnique.Append(dtID.Day);
strUnique.Append((dtID.Year.ToString()).Substring( 2,2));
strUnique.Append(dtID.Hour);
strUnique.Append(dtID.Minute);
strUnique.Append(dtID.Second);
}

public static GenUniqueID Instance
{
return instUnique;
}

public static string UniqueID
{
get
{
return strUnique.ToString();
}
}
}

I'll call this method from my aspx page :

GenUniqueID key=GenUniqueID.Instance();
string strKey=key.UniqueID;

Will this guarantee a UNIQUE key even if two or more users from
different locations will click on "Generate Key" at the same time???

Thanks,
Ashish.

Nov 15 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Tim Clacy | last post by:
Is there such a thing as a Singleton template that actually saves programming effort? Is it possible to actually use a template to make an arbitrary class a singleton without having to: a)...
10
by: E. Robert Tisdale | last post by:
Could somebody please help me with the definition of a singleton? > cat singleton.cc class { private: // representation int A; int B; public: //functions
1
by: Jim Strathmeyer | last post by:
So I'm trying to implement a singleton template class, but I'm getting a confusing 'undefined reference' when it tries to link. Here's the code and g++'s output. Any help? // singleton.h ...
3
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic...
7
by: Ethan | last post by:
Hi, I have a class defined as a "Singleton" (Design Pattern). The codes are attached below. My questions are: 1. Does it has mem leak? If no, when did the destructor called? If yes, how can I...
3
by: Harry | last post by:
Hi ppl I have a doubt on singleton class. I am writing a program below class singleton { private: singleton(){}; public: //way 1
5
by: Pelle Beckman | last post by:
Hi, I've done some progress in writing a rather simple singleton template. However, I need a smart way to pass constructor arguments via the template. I've been suggested reading "Modern C++...
6
by: Manuel | last post by:
Consider the classic singleton (from Thinking in C++): ----------------------------------------------------- //: C10:SingletonPattern.cpp #include <iostream> using namespace std; class...
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
3
by: stevewilliams2004 | last post by:
I am attempting to create a singleton, and was wondering if someone could give me a sanity check on the design - does it accomplish my constraints, and/or am I over complicating things. My design...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.