rest - Java – multiple call on DAO causes exception (wildly, jboss) -
i building java rest application. therefor using jboss on wildfly 8 server.
the following code causes exception: jbas011469: transaction required perform operation (either use transaction or extended persistence context)
@path("/users") @stateless public class usersendpoint { @post @produces(mediatype.application_json) @consumes(mediatype.application_json) public response create(user user) { try { this.checkusername(user.getusername()); this.checkemail(user.getemail()); return response.ok(userdao.create(user)).build();; } catch (ioexception e) { return response.status(response.status.not_acceptable) .entity(e.getmessage()) .build(); } catch (exception e) { return response.status(response.status.not_acceptable) .entity(e.getmessage()) .build(); } } @post @path("username") @produces(mediatype.application_json) @consumes(mediatype.text_html) public response username(string username) { try { this.checkusername(username); return response.ok().build(); } catch (exception e) { return response.status(response.status.not_acceptable) .entity(entity) .build(); } } @post @path("email") @produces(mediatype.application_json) @consumes(mediatype.text_html) public response email(string email) { try { this.checkemail(email); return response.ok().build(); } catch (exception e) { return response.status(response.status.not_acceptable) .entity(this.validator.validateerror(null, e)) .build(); } } private void checkusername(string username) throws ioexception { try { userdao.get(username); throw new ioexception("username taken allready."); } catch (ioexception e) { throw e; } catch (exception e) { system.out.println("username can used"); } } private void checkemail(string email) throws ioexception { try { userdao.getbyemail(email); throw new ioexception("email taken allready."); } catch (ioexception e) { throw e; } catch (exception e) { system.out.println("email can used"); } } }
the public response email(string email)
, public response username(string username)
functions working fine. problem seems call of both functions through public response create(user user)
function:
... try { this.checkusername(user.getusername()); this.checkemail(user.getemail()); ...
so when have correct username duplicate email exception jbas011469: transaction required perform operation (either use transaction or extended persistence context)
going thrown in userdao.getbyemail(email)
. when have duplicate username instead ioexception("username taken allready.")
going thrown expected.
when change order of functions to:
... try { this.checkemail(user.getemail()); this.checkusername(user.getusername()); ...
the same problem occurs in userdao.get(username)
, correct email , duplicate username.
edit
when removing throw new ioexception("username taken allready.");
in private void checkusername(string username) throws ioexception { }
second dao call working fine. problem seems exception thrown. how can solve that?
my userdao
public interface userdao { public user create(user user) throws exception; public user get(string username) throws exception; public user getbyemail(string email) throws exception; }
my userbean
@stateless @remote(userdao.class) public class userbean implements userdao { // injected database connection: @persistencecontext private entitymanager em; @override public user create(user user) throws exception { em.persist(user); return user; } @override public user get(string username) throws exception { return em.createnamedquery(user.query_username, user.class) .setparameter("username", username) .getsingleresult(); } @override public user getbyemail(string email) throws exception { return em.createnamedquery(user.query_email, user.class) .setparameter("email", email) .getsingleresult(); } }
Comments
Post a Comment