-
1. Re: calling finders from the bean class
wchao Feb 20, 2002 8:45 PM (in response to laz777)Why don't you just define the column as unique and have the database handle the problem of enforcing uniqueness? You could make the create method accept an id and username.
-
2. Re: calling finders from the bean class
laz777 Feb 21, 2002 3:06 PM (in response to laz777)I thought of that, but when a new user is created, I need to return (or throw) an error condition back to the calling code, so a page can be displayed prompting the user for a new user id.
I could catch the sql exception that the database will throw if a unique constraint is violated in the db, but wouldn't that then tie the application to a specific database? My experience tells me that I'd get a proprietary error code back in the exception (i.e. oracle is going to give me a different error code than postgres would for violating a unique constraint).
This is quite important since we are developing the app on postgres, but it must scale to either DB2 or Oracle in the future.
-Todd -
3. Re: calling finders from the bean class
raja05 Feb 21, 2002 3:36 PM (in response to laz777)You can make the userid as a primary key.. And catch DuplicateKeyException.
-
4. Re: calling finders from the bean class
jcarroll Feb 22, 2002 10:32 AM (in response to laz777)Also if your field isn't part of the primary key and you are using JBoss 3.0(EJB 2.0) you could use an ejbSelect method to do what you want.
-
5. Re: calling finders from the bean class
laz777 Feb 26, 2002 4:15 AM (in response to laz777)unfortunately, I can't use 3.0 till it's stable...
just for the hell of it, I tried setting the login_name field to unique in the database. However, when a violation happens, the SQLException is thrown on the server side, not on the client side. All I get on the client is a javax.transaction.RollbackException. Which as far as I can tell is useless to me for this application, and the server would still be throwing SQLExceptions.
Anyway, can you throw exceptions in the EJB class that you can then catch in your calling JSPs? I've been looking at RemoteExceptions...
-Todd -
6. Re: calling finders from the bean class
chuang88 Feb 26, 2002 10:39 AM (in response to laz777)From jsp, you can call home.findByPrimaryKey() before calling home.create().
-
7. Re: calling finders from the bean class
laz777 Feb 26, 2002 4:28 PM (in response to laz777)crap, that's pretty obvious... guess I didn't think of that cause it's too damn easy. :)
thanks a bunch chuang, that solves my problem (except I'll call a finder method for the login name findByLoginName() )
just for giggles, does anyone know how to throw exceptions in the bean class, and catch them in the calling jsp? I thought of some other scenarios last night where I might want to do this...
-T -
8. Re: calling finders from the bean class
laz777 Feb 26, 2002 4:51 PM (in response to laz777)hmmm... There is a problem with checking uniqueness with a findBy method in the jsp. There is a *very* small chance that while the jsp was doing the search for a username, another user could submit that username, and the code wouldn't detect it.
This will still work for my purposes, but if you needed to enforce uniqueness on a piece of data that had a higher probability of collision, then it wouldn't be precise enough.
-T -
9. Re: calling finders from the bean class
laz777 Mar 11, 2002 2:54 AM (in response to laz777)I ended up using a much more optimal solution (that should get rid of the chance for race conditions...)
in the setLoginName method of my ejb class, I get the home interface of my bean and invoke a finder to find duplicates, if one is found, I throw an exception.
quick note on throwing exceptions from your ejb class:
you must add a throws clause to your main interface, and any create methods in your home interface that call the throwing code.
(your compiler will not remind you about any exceptions you've forgotten to throw in your home class)
-Todd