12 Replies Latest reply on May 25, 2003 10:47 PM by xiaoyi

    JDBC XAManagedConnection and XAResource

    Kevin Birch Newbie

      I want to get a reference to the XAResource that is used by (is) the XAManagedConnection for the JDBC outbound resource adapter. However, it seems that there is no difference in the client view of a XA DataSource and a non-XA DataSource, since they are both wrapped up in WrappedDataSource and WrappedConnection.

      Is there any way I can get access to the XAResource if I have an XA DataSource set up in JBoss? I am using JBoss 4.0 and the oracle-xa-ds.xml, BTW.

        • 1. Re: JDBC XAManagedConnection and XAResource
          David Jencks Master

          If you are a client application, there is no way, and that is a good thing. If you are the resource adapter, you obviously can get it, since you are supplying it. If you are the jboss connection manager, you already get it. So if you really need it, you need to be the resource adapter or the connection manager.

          What do you want to do with it?

          • 2. Re: JDBC XAManagedConnection and XAResource
            Kevin Birch Newbie

            Well, the code that will be using this is an inbound resource adapter. I am trying to start a global transaction that involves an Oracle connection and a message driven bean that then speaks to SonicMQ.

            So, I want to give the XAResource from the JDBC XAConnection to MessageEndpointFactory.createEndpoint() to enlist the XAConnection in the global transaction. I am trying to avoid creating my own impl of XAResource, since I am not in complete control of the JDBC connection's transaction. It is being delegated to Oracle's AQ API, which handles all commits and rollbacks over the JDBC connection. But the AQ API can be effected by the transaction context of the JDBC XAConnection it is using.

            • 3. Re: JDBC XAManagedConnection and XAResource
              David Jencks Master

              What is the outbound adapter in this scenario? Are you writing an inbound-outbound adapter? I don't understand yet how the outbound adapter gets the XAResource.

              • 4. Re: JDBC XAManagedConnection and XAResource
                Kevin Birch Newbie

                In this case, the outbound adapter is not being used by the same MDB that is receiving messages from the inbound adapter, so they are part of separate transactions. When the MDB is activated by the inbound RA, it places a JMS message in a JMS queue, and is done.

                So there is an inbound adapter, and an outbound adapter. However, for the moment, I'm trying to get the global transaction between the inbound RA (which talks to Oracle AQ using a JDBC connection from the Oracle XA DataSource) and the MDB that it activates to work.

                Is there any other way to configure the deployment of the XA DataSource so that a client (my inbound RA) can get access to the JDBC XAConnection object rather than the WrappedConnection, which only seems to have access to the XAConnection's java.sql.Connection object.

                thanks,
                kevin

                • 5. Re: JDBC XAManagedConnection and XAResource
                  David Jencks Master

                  So, are you extending the jboss xa jca-jdbc wrapper to be an inbound adapter for Oracle AQ?

                  And the inbound component gets a connection from the datasource bound in jndi, in no transaction context?

                  And you want to get the XAResource for the associated ManagedConnection so it will be enrolled in the CMT transaction started by the MDB message delivery?

                  I think this will be OK as long as the message delivery takes place in a different thread than you got the connection in. If it is the same thread you may end up trying to enroll the xa resource twice.

                  Assuming this is close to what you are trying to do, I'd modify the WrappedConnection with a getManagedConnection method, and get the XAResource from that. Today at least I don't have a strong objection to your adding such a method in cvs, since probably you won't be the only person wanting to do something like this.

                  • 6. Re: JDBC XAManagedConnection and XAResource
                    Kevin Birch Newbie

                    Actually I have written my own inbound RA to communicate with Oracle AQ, it just uses a deployed Oracle XA DataSource to retrieve a connection when an endpoint is activatated.

                    Yes, message delivery happens in a seperate thread, so I don't think that will be a problem.

                    Ok, I was thinking of maybe adding a method WrappedConnection.getXAResource() that would be call-through to ManagedConnection.getXAResource().

                    Thanks,
                    kevin

                    • 7. Re: JDBC XAManagedConnection and XAResource
                      David Jencks Master

                      After considering this some more I think that exposing either the managed connection or the xaresource in a public method is a bad idea and will only lead to user confusion. Can you turn this into a protected getManagedConnection() method and subclass the XAManagedConnectionFactory to return a subclass of WrappedConnection with your getXAResource method?

                      You can deploy such a ManagedConnectionFactory with the xa-datasource element using the managed-connection-factory-class element.

                      This will be somewhat more work for you but I'm really worried about what will happen if users can get a XAResource too easily.

                      • 8. Re: JDBC XAManagedConnection and XAResource
                        Kevin Birch Newbie

                        Sure, that's no problem, but if I understand what you mean, XAManagedConnectionFactory will override getConnection in BaseWrapperManagedConnection and return something like an XAWrappedConnection that has the method getXAResource().

                        In that case, what's the difference between someone writing:

                        ((WrappedConnection)c).getXAResource();

                        and:

                        ((XAWrappedConnection)c).getXAResource();

                        Both methods require that the developer know something about the WrappedConnection class, and that they have a reason to get the XAResource.

                        It's the same scenario as if they get the underlying connection and close it. The handle won't know that it has been closed, and it could behave unexpectedly or leak a resource.

                        kevin

                        • 9. Re: JDBC XAManagedConnection and XAResource
                          Kevin Birch Newbie

                          Sure, that's no problem, but if I understand what you mean, XAManagedConnectionFactory will override getConnection in BaseWrapperManagedConnection and return something like an XAWrappedConnection that has the method getXAResource().

                          In that case, what's the difference between someone writing:

                          ((WrappedConnection)c).getXAResource();

                          and:

                          ((XAWrappedConnection)c).getXAResource();

                          Both methods require that the developer know something about the WrappedConnection class, and that they have a reason to get the XAResource.

                          It's the same scenario as if they get the underlying connection and close it. The handle won't know that it has been closed, and it could behave unexpectedly or leak a resource.

                          kevin

                          • 10. Re: JDBC XAManagedConnection and XAResource
                            David Jencks Master

                            I'm suggesting that the implementation from jboss not expose anything unnecessary, but that you make it more extensible with the protected getManagedConnection method. Then for your adapter you can write and extension that has the getXAResource method you need. Then, people using the jboss standard version still can't hurt themselves.

                            • 11. Re: JDBC XAManagedConnection and XAResource
                              xiaoyi Newbie

                              Hi:
                              kbirch,davidjencks
                              I want to connect websphere
                              with jboss.So that complish transfering money between two databases in DTP.
                              Could you tell me how to create a resource adapter based JCA?
                              I am a student and i must complish within 20 days.
                              Could you send your adapter to me?I want to get some spirit from yours and i will complish in distributed transaction process.Or some code for XA based JCA.
                              thanks!
                              my email is li.xiaotantg@zte.com.cn

                              • 12. Re: JDBC XAManagedConnection and XAResource
                                xiaoyi Newbie

                                Hi:
                                kbirch,davidjencks
                                I want to connect websphere
                                with jboss.So that complish transfering money between two databases in DTP.
                                Could you tell me how to create a resource adapter based JCA?
                                I am a student and i must complish within 20 days.
                                Could you send your adapter to me?I want to get some spirit from yours and i will complish in distributed transaction process.Or some code for XA based JCA.
                                thanks!
                                my email is li.xiaotantg@zte.com.cn