Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Enlisting Custom DataSource with JTa

  1. #1
    Join Date
    Aug 2004
    Posts
    105

    Default Enlisting Custom DataSource with JTa

    Hello,
    We are building an application which uses a legacy database schema on Oracle. Security is managed in database by using USER function of oracle sql, so in order to get records, update or delete record we need that the user is connected from his original username/password and also every operation should be performed on a connection with original user name and password. To achieve this and connection pooling we are using OCIConnectionPool implementation and Oracle Proxy connections.

    The problem is that we have made our own datasource so it did not enlist itself with ongoing jta transaction. So please tell us the way to enlist with it with ongoing jta transaction.

    Regards,
    Shoaib Akhtar
    Regards,
    Shoaib Akhtar

  2. #2
    Join Date
    Aug 2004
    Posts
    1,104

    Default

    What is your application server/JTA implementation?
    Thomas Risberg
    SpringSource by Pivotal
    http://www.springsource.org

  3. #3
    Join Date
    Aug 2004
    Posts
    105

    Default

    Hello,
    We are using JBoss 3.2.3, SLSSB EJB + CMT , Hibernate + Spring

    Regards,
    Shoaib Akhtar
    Regards,
    Shoaib Akhtar

  4. #4
    Join Date
    Aug 2004
    Posts
    1,104

    Default

    Do you have a JBoss JCA datasource configuration file set up for the OCIConnectionPool? Does the OCIConnectionPool support JCA?
    Thomas Risberg
    SpringSource by Pivotal
    http://www.springsource.org

  5. #5
    Join Date
    Aug 2004
    Posts
    105

    Default RE

    Hello,
    Thanks trisberg for your reply. As far i know OCIConnectionPool do not ship with JCA adapter. Can you help me in building JCA adapetr for it? I have never worked with JCA. My datasource is as follows

    OracleOCIConnectionPoolDataSource.java
    Code:
    /*
     * Copyright 2002-2004 the original author or authors.
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    
    package org.springframework.jdbc.oracle;
    
    import java.sql.*;
    import java.util.*;
    
    import org.springframework.jdbc.*;
    import org.springframework.jdbc.datasource.*;
    import org.springframework.util.*;
    import oracle.jdbc.driver.*;
    import oracle.jdbc.pool.*;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    
    /**
     * Implementation of SmartDataSource that configures an OracleOCIConnectionPool
     * via bean properties, and returns a new connection every time.
     *
     * @author Guido Schmutz
     * @since 12.05.2004
     *
     * @version $Id: OracleOCIConnectionPoolDataSource.java,v 1.6 2004/05/31 11:53:56 sakhtar Exp $
     * @see org.springframework.jndi.JndiObjectFactoryBean
     * @see org.apache.commons.dbcp.BasicDataSource
     */
    public class OracleOCIConnectionPoolDataSource
        extends AbstractDataSource
        implements SmartDataSource
    {
    
      private final Log logger = LogFactory.getLog(OracleOCIConnectionPoolDataSource.class);
    
      private OracleOCIConnectionStrategy connectionStrategy = null;
    
      private String connPoolMinLimit = "1";
      private String connPoolMaxLimit = "4";
      private String connPoolIncrement = "1";
      private String driverType = "oci8";
      private String tnsName = null;
      private String url = null;
      private String username = null;
      private String password = null;
    
    
      // ocipool of this instance
      private OracleOCIConnectionPool ocipool = null;
      private String connPoolTimeOut = "20";
    
      public void setConnectionStrategy(OracleOCIConnectionStrategy strategy)
      {
        this.connectionStrategy = strategy;
      }
    
      /**
       * @return Returns the connPoolIncrement.
       */
      public String getConnPoolIncrement()
      {
        return connPoolIncrement;
      }
    
      /**
       * @param connPoolIncrement The connPoolIncrement to set.
       */
      public void setConnPoolIncrement(String connPoolIncrement)
      {
        this.connPoolIncrement = connPoolIncrement;
      }
    
      /**
       * @return Returns the connPoolMaxLimit.
       */
      public String getConnPoolMaxLimit()
      {
        return connPoolMaxLimit;
      }
    
      /**
       * @param connPoolMaxLimit The connPoolMaxLimit to set.
       */
      public void setConnPoolMaxLimit(String connPoolMaxLimit)
      {
        this.connPoolMaxLimit = connPoolMaxLimit;
      }
    
      /**
       * @return Returns the connPoolMinLimit.
       */
      public String getConnPoolMinLimit()
      {
        return connPoolMinLimit;
      }
    
      /**
       * @param connPoolMinLimit The connPoolMinLimit to set.
       */
      public void setConnPoolMinLimit(String connPoolMinLimit)
      {
        this.connPoolMinLimit = connPoolMinLimit;
      }
    
      /**
       * @return Returns the driverType.
       */
      public String getDriverType()
      {
        return driverType;
      }
    
      /**
       * @param driverType The driverType to set.
       */
      public void setDriverType(String driverType)
      {
        this.driverType = driverType;
      }
    
      /**
       * @return Returns the password.
       */
      public String getPassword()
      {
        return password;
      }
    
      /**
       * @param password The password to set.
       */
      public void setPassword(String password)
      {
        this.password = password;
      }
    
      /**
       * @return Returns the tnsName.
       */
      public String getTnsName()
      {
        return tnsName;
      }
    
      /**
       * @param tnsName The tnsName to set.
       */
      public void setTnsName(String tnsName)
      {
        this.tnsName = tnsName;
      }
    
      /**
       * @return Returns the url.
       */
      public String getUrl()
      {
        return url;
      }
    
      /**
       * @param url The url to set.
       */
      public void setUrl(String url)
      {
        this.url = url;
      }
    
      /**
       * @return Returns the username.
       */
      public String getUsername()
      {
        return username;
      }
    
      public String getConnPoolTimeOut() {
        return connPoolTimeOut;
      }
    
      /**
       * @param username The username to set.
       */
      public void setUsername(String username)
      {
        this.username = username;
      }
    
      public void setConnPoolTimeOut(String connPoolTimeOut) {
        this.connPoolTimeOut = connPoolTimeOut;
      }
    
      /**
       * Constructor for bean-style configuration.
       */
      public OracleOCIConnectionPoolDataSource()
      {
      }
    
      /**
       * Create a new SingleConnectionDataSource with the given standard
       * DriverManager parameters.
       */
      public OracleOCIConnectionPoolDataSource(String url, String username,
                                               String password) throws
          CannotGetJdbcConnectionException
      {
        setUrl(url);
        setUsername(username);
        setPassword(password);
      }
    
      /**
       * This DataSource returns a new connection every time: Close it when returning one to the "pool".
       */
      public boolean shouldClose(Connection conn)
      {
        return true;
      }
    
      public Connection getConnection() throws SQLException
      {
        return getConnectionFromPool();
      }
    
      public Connection getConnection(String username, String password) throws
          SQLException
      {
        if (ObjectUtils.nullSafeEquals(username, getUsername()) &&
            ObjectUtils.nullSafeEquals(password, getPassword()))
        {
          return getConnection();
        }
        else
        {
          throw new SQLException(
              "OCIConnectionPoolDataSource does not support custom username and password");
        }
      }
    
      protected OracleConnection getConnectionFromPool() throws SQLException
      {
        OracleConnection conn = null;
    
        if (ocipool == null)
        {
          initPool();
        }
    
        if ( (connectionStrategy != null) && (connectionStrategy.getProxyUserName() != null))
        {
          if (connectionStrategy.getProxyUserName() != null &&
              connectionStrategy.getProxyUserPassword() != null)
          {
    
            conn = (OracleConnection) ocipool.getConnection(connectionStrategy.
                getProxyUserName(), connectionStrategy.getProxyUserPassword());
            logger.info("Given connecttion to user: " +
                               connectionStrategy.getProxyUserName() +
                               " password: " +
                               connectionStrategy.getProxyUserPassword());
          }
    
        }
        else
        {
          conn = (OracleConnection) ocipool.getConnection();
          logger.warn("Given default connection");
        }
    
        if ( (connectionStrategy != null) &&
            (connectionStrategy.getClientIdentifier() != null))
        {
          conn.setClientIdentifier(connectionStrategy.getClientIdentifier());
        }
    
        return conn;
      }
    
      /**
       * Initializes the connection pool
       * @throws SQLException
       */
      private void initPool() throws SQLException
      {
        // Create the pool
        ocipool = new OracleOCIConnectionPool();
    
        // Configure the Datasource
        configureDataSource();
    
        // Configure the connection pool
        configPool();
      }
    
      /**
       * Configures the DataSource with the driverTpye to use, user name, password, tnsEntry name
       * and URL
       * @throws SQLException
       */
      private void configureDataSource() throws SQLException
      {
        // Driver type either 'thin' or 'oci8'
        ocipool.setDriverType(this.driverType);
    
        // TNS Entry in case of oci8
        ocipool.setTNSEntryName(this.tnsName);
    
        // URL in case of thin
        ocipool.setURL(this.url);
    
        // user name
        ocipool.setUser(this.username);
    
        // password
        ocipool.setPassword(this.password);
      }
    
      /**
       * Configures the OCI Connection pool by setting the OCI Connectoin pool parameters
       * @throws SQLException
       */
      private void configPool() throws SQLException
      {
    
        Properties poolProp = new Properties();
    
        // the minimum number of physical conections maintained by the pool
        poolProp.put(OracleOCIConnectionPool.CONNPOOL_MIN_LIMIT,
                     this.connPoolMinLimit);
    
        // the maximum number of physical conections maintained by the pool
        poolProp.put(OracleOCIConnectionPool.CONNPOOL_MAX_LIMIT,
                     this.connPoolMaxLimit);
    
        // Incremental number of physical connections to be opended wehn all the existing ones
        // are busy and a new connection is requested.
        poolProp.put(OracleOCIConnectionPool.CONNPOOL_INCREMENT,
                     this.connPoolIncrement);
        poolProp.put(OracleOCIConnectionPool.CONNPOOL_TIMEOUT, this.connPoolTimeOut);
        ocipool.setPoolConfig(poolProp);
      }
    }
    OracleOCIConnectionStrategy
    Code:
    /*
     * Created on 12.05.2004
     *
     * TODO To change the template for this generated file go to
     * Window - Preferences - Java - Code Generation - Code and Comments
     */
    package org.springframework.jdbc.oracle;
    
    /**
     * @author gus
     *
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Generation - Code and Comments
     */
    public interface OracleOCIConnectionStrategy
    {
      public abstract String getProxyUserName();
    
      public abstract void setProxyUserName(String userName);
    
      public abstract String getProxyUserPassword();
    
      public abstract void setProxyUserPassword(String password);
    
      public abstract String getRoles();
    
      public abstract void setRoles(String roles);
    
      public abstract String getClientIdentifier();
    
      public abstract void setClientIdentifier(String clientId);
    }
    OracleOCIConnectionThreadLocalStrategy.java
    Code:
    /*
     * Copyright 2002-2004 the original author or authors.
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    
    package org.springframework.jdbc.oracle;
    
    import java.util.*;
    
    /**
     * The implementation of the OracleOCIConnectoinStrategy interface. It's implemented as a thread
     * local so everything stored in such an instance is kept seperate for each thread.
     *
     *  * @author Guido Schmutz
     */
    public class OracleOCIConnectionThreadLocalStrategy
        implements OracleOCIConnectionStrategy
    {
      private static class ConnectionThreadLocal
          extends ThreadLocal
      {
    
        private final static String USERNAME = "username";
        private final static String PWD = "pwd";
        private final static String ROLES = "roles";
        private final static String CLIENT_ID = "clientID";
    
        protected Object initialValue()
        {
          return new HashMap();
        }
    
        private HashMap getHashMap()
        {
          return (HashMap)super.get();
        }
    
        public String getProxyUserName()
        {
          return (String) getHashMap().get(USERNAME);
        }
    
        public void setProxyUserName(String userName)
        {
          getHashMap().put(USERNAME, userName);
        }
    
        public String getProxyUserPassword()
        {
          return (String) getHashMap().get(PWD);
        }
    
        public void setProxyUserPassword(String password)
        {
          getHashMap().put(PWD, password);
        }
    
        public String getRoles()
        {
          return (String) getHashMap().get(ROLES);
        }
    
        public void setRoles(String roles)
        {
          getHashMap().put(ROLES, roles);
        }
    
        public String getClientIdentifier()
        {
          return (String) getHashMap().get(CLIENT_ID);
        }
    
        public void setClientIdentifier(String clientId)
        {
          getHashMap().put(CLIENT_ID, clientId);
        }
      }
    
      private static ConnectionThreadLocal connThreadLocal = new
          ConnectionThreadLocal();
    
      public String getProxyUserName()
      {
        return connThreadLocal.getProxyUserName();
      }
    
      public void setProxyUserName(String userName)
      {
        connThreadLocal.setProxyUserName(userName);
      }
    
      public String getProxyUserPassword()
      {
        return connThreadLocal.getProxyUserPassword();
      }
    
      public void setProxyUserPassword(String password)
      {
        connThreadLocal.setProxyUserPassword(password);
      }
    
      public String getRoles()
      {
        return connThreadLocal.getRoles();
      }
    
      public void setRoles(String roles)
      {
        connThreadLocal.setRoles(roles);
      }
    
      public String getClientIdentifier()
      {
        return connThreadLocal.getClientIdentifier();
      }
    
      public void setClientIdentifier(String clientId)
      {
        connThreadLocal.setClientIdentifier(clientId);
      }
    }
    Regards,
    Shoaib Akhtar

  6. #6
    Join Date
    Aug 2004
    Posts
    105

    Default Re

    Hi all,
    Can anybody help me?
    Regards,
    Shoaib Akhtar

  7. #7
    Join Date
    Aug 2004
    Posts
    1,104

    Default

    This is more of a JBoss/Oracle issue. You need to set up a DataSource that JBoss and its transaction manager recognizes before you can do any EJB/JTA transactions. Have you checked the JBoss forum?
    Thomas Risberg
    SpringSource by Pivotal
    http://www.springsource.org

  8. #8
    Join Date
    Aug 2004
    Posts
    105

    Default Re

    Hi,
    Thanks for your response. Actually JBoss dont support custom datasources ( as ours) beacuse we need our own connection pool not created by JBoss. Kindly just guide me a bit e.g. should i make my own jca adapter so that my datasource enlist with JTA.
    Regards,
    Shoaib Akhtar

  9. #9
    Join Date
    Aug 2004
    Posts
    105

    Default

    Hi all,
    Can anybody help me?
    Regards,
    Shoaib Akhtar

  10. #10
    Join Date
    Aug 2004
    Posts
    4

    Default custom datasource pool

    hi shaby,

    may be i am coming to late ... but ...

    i did something like that, now i am working with a friend on a library that will help to customize all that ... We did a pool of XAConnections (xadatsource) and of course a pool of simple sql connections. We found out how to enable a normal jdbc driver, and to make an XA of it, so it can work with mysql per example, which does not provide a XA enable driver, and that mysql resource can participate in a XA transaction.

    I did some tests with hibernate to see if that could work together and it seems to work.

    How does it work, we have a class that provides you a pool of connections, of course it is possible to bind it into the jndi ... this pool is configurable as you want, mawimum connections, minum open connections in the pool, jdbc stuffs and everything needed ...
    You can get an XAConnection from the pool and enlist it in a JTA transaction (Jboss has it).

    I am not gonna talk anymore because i do not understand what you really need ... can you explain exactly, i ll do my best to help you ...

    Anyway we just started to work on that library so it is not yet disponible.

Similar Threads

  1. Replies: 2
    Last Post: Aug 2nd, 2006, 10:18 PM
  2. Odd behaviour when injecting TransactionTemplate
    By damon311 in forum Container
    Replies: 3
    Last Post: Jul 23rd, 2005, 11:21 AM
  3. Replies: 1
    Last Post: Feb 12th, 2005, 07:30 AM
  4. Custom Datasource
    By chenrici in forum Data
    Replies: 1
    Last Post: Oct 28th, 2004, 12:25 PM
  5. Ignoring missing Jndi DataSource within IDE?
    By Bill Pearce in forum Container
    Replies: 2
    Last Post: Oct 27th, 2004, 09:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •