[EJB 3.0] - transaction problem
by Sukar1205
I am trying to run a client class to test the CustomerBean class that I wrote but its giving me an error which mentions EntityManager must be access within a transaction. I am new to EJB3 so bear with me please.
This the code for my customerBean class.
| @Stateless
| public class CustomerBean implements CustomerRemote {
|
| @Resource(mappedName = "java:/SQL")
| private DataSource SQL;
|
| @PersistenceContext private EntityManager em;
|
| public void addCustomer(TempCustomer a){
| em.persist(a); //This is where the Exception occurs ..thanks
| }
|
| public TempCustomer findCustomerbyID(int id) {
| return em.find(TempCustomer.class, id);
| }
|
| public void deleteCustomer(TempCustomer a) {
| em.remove(a);
| }
|
| public void deleteID(int id){
| em.remove(id);
| }
|
|
And this is my client class to test my CustomerBean class... I am just trying to add 2 customers in the table.
| public class Client {
| public static void main(String[] args) throws Exception
| {
| Connection conn;
| // Step 1: Load the JDBC driver.
| Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
| System.out.println("Driver Loaded.");
| // Step 2: Establish the connection to the database.
| String url = "jdbc:sqlserver://localhost:1433;database=jdocxpt";
| conn = DriverManager.getConnection(url, "sa", "");
| System.out.println("Got Connection.");
|
| Context ctx = getInitialContext();
| CustomerRemote c = (CustomerRemote) ctx.lookup("CustomerBean/remote");
|
| TempCustomer first = new TempCustomer();
| TempCustomer second = new TempCustomer();
|
| System.out.println("adding Customers");
| first.setId(20);
| first.setName("aaaa");
| second.setId(21);
| second.setName("kkkk");
| c.addCustomer(first);
| c.addCustomer(second);
| }
I would appreciate it if some could help me with this transaction problem.
THANKS IN ADVANCE.
the error is
| Exception in thread "main" javax.ejb.EJBTransactionRolledbackException: EntityManager must be access within a transaction
| at org.jboss.ejb3.tx.Ejb3TxPolicy.handleInCallerTx(Ejb3TxPolicy.java:87)
| at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:130)
| at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:195)
| at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
| at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
| at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
| at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62)
| at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
| at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:77)
| at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:106)
| at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
| at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:46)
| at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
| at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
| at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
| at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:278)
| at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:106)
| at org.jboss.aspects.remoting.AOPRemotingInvocationHandler.invoke(AOPRemotingInvocationHandler.java:82)
| at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:734)
| at org.jboss.remoting.transport.socket.ServerThread.processInvocation(ServerThread.java:560)
| at org.jboss.remoting.transport.socket.ServerThread.dorun(ServerThread.java:369)
| at org.jboss.remoting.transport.socket.ServerThread.run(ServerThread.java:165)
|
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4086302#4086302
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4086302
18 years, 10 months
[EJB 3.0] - Re: Simplest possible stateless bean
by Renen
anonymous wrote : Maybe there is an alternative that's missing.
I must say, I found myself asking that. We use application servers because we always have. And, it used to be neccesary. I'm not sure it still is.
;-) But I suppose one shouldn't get too philosophical.
We have a straight forward scenerio where we host two or three web sites, running against a common database. We use EJBs to manage database access, support the creation of the web pages and the rendering of data. And, we rely on MDBs, JSPs and JSP Tags.
We serve about 100 000 pages per day.
So, that's the broad context. More specifically, I would like to take advantage of some of the Java 1.5 constructs (specifically, varargs), and would also like to move with the times. So, I would like start porting our existing J2EE EJBs to EJB3.
POJOs have a lot of appeal in that they are simple to implement, easy to test etc etc. But, they never used to be managed in the same way that application server code was. But, I think that might have changed. Certainly, Tomcat must deal with them very effectively.
So, should we simply abandon EJBs all together? Feels a bit dramatic.
;-)
anonymous wrote : How would you like to reference your bean from a client (or servlet)?
The EJBs are accessed from JSP pages, tag libraries, MDBs and from other EJBs (all of which are stateless session beans). All these objects exist within JARs or WARs inside a single ear running on a seperate JBOSS instance.
Once implemented, the EJBs won't change very fast. Although we do add MDBs at a steady rate.
anonymous wrote : Again a JNDI name needs to be unique. And what if you had multiple bussiness interfaces, should we 'polute' JNDI by binding them all?
I'll hear that. Although, after mulling it over for bit, does it make any difference if JNDI is overpopulated? Are those performance concerns material against the singificantly increased levels of complexity?
THis topic has meandered quite far. I have learnt quite a bit and the replies have made me think. Thanks.
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4086295#4086295
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4086295
18 years, 10 months