7 Replies Latest reply on Jul 7, 2003 2:30 PM by ioparra

    there is a wrong message that: java.lang.ClassCastException

    xiaoyi

      i want to connect to the db2 datasource that supports xa .
      the call sentence is: javax.sql.XADataSource ds = (XADataSource)ctx.lookup("java:/DB2XADS");
      but the case wrong message is:
      java.lang.ClassCaseException.
      Please help me !
      thanks!

        • 1. Re: there is a wrong message that: java.lang.ClassCastExcept
          ioparra

          Does it say, ClassCastException(javax.sql.DataSource)?

          Even though you know that the datasource is an XADatasource, your not given it during the lookup. Instead your given a wrapper that implements Datasource that hooks into JBoss and controls Security/Transaction for your Datasource(ie. JCA).

          Try:

          DataSource ds = (DataSource)ctx.lookup(...).


          This also add the extra perk of switching to a non-xa driver without having to change your code everywhere.

          G/L
          -Ivan

          • 2. Re: there is a wrong message that: java.lang.ClassCastExcept
            xiaoyi

            it only says:java.lang.ClassCastException.
            no other.
            in the db2-xa-ds.xml ,i write:
            <xa-datasource>
            <jndi-name>DB2XADS</jndi-name>

            <xa-datasource-class>COM.ibm.db2.jdbc.DB2XADataSource</xa-datasource-class>
            <xa-datasource-property name="DatabaseName">account</xa-datasource-property>
            <xa-datasource-property name="User">db2</xa-datasource-property>
            <xa-datasource-property name="Password">db2</xa-datasource-property>
            </xa-datasource>
            if write:
            DataSource ds = (DataSource)ctx.lookup(...).
            then the db2 only supports the 1pc.and it dose not the xadatasource.
            but now i want to assign it to do with a distributed transaction.

            • 3. Re: there is a wrong message that: java.lang.ClassCastExcept
              ioparra

              The datasource you get is a Datasource Wrapper around the XA stuff. If you do a:

              System.out.println(ds.getClass());

              You'll probably get some Jboss wrapper class.

              if you do
              System.out.println(ds.getConnection().getClass());

              You'll get the class from
              XADataSource().allocateManagedConnection().getConnection().

              I think its allocateManagedConnection, haven't looked at the XA spec recently.

              • 4. Re: there is a wrong message that: java.lang.ClassCastExcept
                xiaoyi

                It's very strange!
                i write the code:
                XADataSource ds = (XADataSource)ctx.lookup("java:/DB2XADS");
                System.out.print(ds.getXAConnection().getClass());
                but when it run on jboss.
                the wrong message is still.and the class infermation that should be shown ,doesn't be shown.
                message is:
                java.lang.CastClassException.

                • 5. Re: there is a wrong message that: java.lang.ClassCastExcept
                  ioparra

                  Can you attach your stack trace and a snippet of code that is creating the CCE?

                  Thanks.

                  -Ivan

                  • 6. Re: there is a wrong message that: java.lang.ClassCastExcept
                    xiaoyi

                    the jsp that call the bmt :
                    Context ctx = new InitialContext();
                    bmtbeanHome home = ( bmtbeanHome)PortableRemoteObject.narrow(ctx.lookup("bmtbean"),test.bmtbeanHome.class);
                    bmtbeanRemote bean = home.create();
                    bean.test();
                    the bmt bean code:
                    try
                    { UserTransaction ut=sessionContext.getUserTransaction();
                    ut.begin();
                    Context ctx = null;
                    Connection conn = null;
                    PreparedStatement stmt = null;
                    ctx = new InitialContext();
                    XADataSource ds = (XADataSource)ctx.lookup("java:/DB2XADS");
                    javax.sql.XAConnection con = ds.getXAConnection();
                    System.out.println("xa class"+con.getClass());
                    java.sql.Connection c=con.getConnection();
                    System.out.println("class"+c.getClass());
                    Statement s = c.createStatement();
                    ResultSet rs = s.executeQuery("select * from protonaccount");
                    if(rs.next())
                    {
                    name = rs.getString("customer_name");
                    System.out.println(name);
                    }
                    s.close();
                    con.close();
                    Hashtable env=new Hashtable();
                    env.put(Context.INITIAL_CONTEXT_FACTORY,
                    "org.jnp.interfaces.NamingContextFactory");
                    env.put(Context.URL_PKG_PREFIXES,
                    "org.jnp.interfaces");
                    env.put(Context.PROVIDER_URL, "10.51.17.3");
                    //env.put(Context.PROVIDER_URL, "127.0.0.1");
                    InitialContext ctx1=new InitialContext(env);
                    Object ob=ctx1.lookup("cmtbean");
                    cmtbeanHome home=(cmtbeanHome)javax.rmi.PortableRemoteObject.narrow(ob,cmtbeanHome.class);
                    cmtbeanRemote bean=home.create();
                    bean.test();
                    ut.rollback();
                    }
                    catch(Exception e)
                    {
                    System.out.println("Exception in cmtbean".concat(String.valueOf(String.valueOf(e))));
                    }

                    the wrong message:

                    09:45:36,799 INFO [STDOUT] Exception in cmtbeanjava.lang.ClassCastException
                    09:45:36,799 ERROR [TxSupport] Application error, UserTransaction not complete o
                    n return of call to stateless session bean -264729354
                    09:45:36,799 WARN [jbossweb] WARNING: Exception for /remote/ds.jsp: java.rmi.Re
                    moteException: Application error, UserTransaction not complete on return of call
                    to stateless session bean -264729354
                    but when i use the codes "DataSource ds=(DataSource)ctx.lookup("java:/DB2DS")"
                    that replace the " XADataSource ds = (XADataSource)ctx.lookup("java:/DB2XADS");"
                    the process is true.
                    below is the db2-xa-ds.xml
                    <xa-datasource>
                    <jndi-name>DB2XADS</jndi-name>

                    <xa-datasource-class>COM.ibm.db2.jdbc.DB2XADataSource</xa-datasource-class>
                    <xa-datasource-property name="DatabaseName">account</xa-datasource-property>
                    <xa-datasource-property name="User">db2</xa-datasource-property>
                    <xa-datasource-property name="Password">db2</xa-datasource-property>
                    </xa-datasource>
                    thanks!

                    • 7. Re: there is a wrong message that: java.lang.ClassCastExcept
                      ioparra

                      2 things.

                      1) Remove the XA's
                      in other words.

                      DataSource ds = (DataSource)ctx.lookup("java:/DB2XADS");

                      The datasource you will see is a wrapper, when you call getConnection on the datasource, the wrapper will call getXAConnection().getConnection on the real XAConnection.

                      2) On the try/catch, try ex.printStackTrace to make sure it is the DS class cast that is throwing the exception.

                      G/L
                      Ivan