Sorry, the cds just wraps a connection as a DataSource.  Nothing fancy.

As for getting the connection, here's what I do.  Don't know if it's the best 
way.

John

    /**
     * Supports checkSchema.  Connection needs to be closed via broker, so we
     * need to proxy the connection and close the source when requested.
     */
    public Connection getConnection() {
        PersistenceBroker broker = 
PersistenceBrokerFactory.defaultPersistenceBroker();
        try {
            Connection con = 
broker.serviceConnectionManager().getConnection();

            InvocationHandler handler = new ConnectionInvocationHandler( 
broker, con );
            Connection result = (Connection) 
Proxy.newProxyInstance(Connection.class.getClassLoader(),
                                          new Class[] { Connection.class },
                                          handler);

            return result;

        } catch ( LookupException le ) {
            throw new RuntimeException(le);
        }
    }

    private static class ConnectionInvocationHandler implements 
InvocationHandler {

        private PersistenceBroker _broker;
        private Connection _con;

        private ConnectionInvocationHandler( PersistenceBroker broker, 
Connection con ) {
            _broker = broker;
            _con = con;
        }

        public Object invoke(Object proxy, Method method, Object[] args) 
throws Throwable {
            Object result = null;
            if ( "close".equals(method.getName()) ) {
                System.out.println("closing connection on broker");
                //Connection.close() returns void, leave result null
                //don't commit - not in transaction
               _broker.close();
            } else {
                result = method.invoke( _con, args );
            }
            return result;
        }

    } //ConnectionInvocationHandler


    private static class ConnectionDataSource implements DataSource {
        private Connection _cn;

        public ConnectionDataSource( Connection cn ) {
            _cn = cn;
        }

        /**
         * @see javax.sql.DataSource#getConnection()
         */
        public Connection getConnection() throws SQLException {
                return _cn;
        }

        //all other methods throw SQLException("Not supported")
    }



>===== Original Message From Gus Heck <[EMAIL PROTECTED]> =====
>For the moment I just implemented my own ConnectionDataSource, but I
>cant seem to get far enough to test it. I dont' seem to be able to get
>hold of a Connection object. Here's what I tried, (if anyone has a
>better way, I would really like to hear it:
>
>        InputStream is =
>Thread.currentThread().getContextClassLoader().getResourceAsStream("project_s
chema.xml");
>        Database db = null;
>        try {
>            DatabaseReader reader = new DatabaseReader();
>            db = (Database) reader.parse( is );
>        } catch (IntrospectionException ie) {
>            System.out.println("Error creating DatabaseReader for
>project_schema.xml");
>        } catch (IOException ioe ) {
>            System.out.println("Error loading project_schema.xml");
>        } catch (SAXException saxe ) {
>            System.out.println("Error parsing project_schema.xml");
>        }
>
>        ConnectionFactoryFactory cff =
>ConnectionFactoryFactory.getInstance();
>        ConnectionFactory cf = cff.createConnectionFactory();
>
>        MetadataManager mm = MetadataManager.getInstance();
>        ConnectionRepository cRepos = mm.connectionRepository();
>        JdbcConnectionDescriptor descriptor = cRepos.getDescriptor(new
>PBKey("fdbcon"));
>
>        System.out.println("descriptor = " + descriptor);
>
>        Connection existing;
>        try {
>            existing = cf.lookupConnection(descriptor);
>        } catch (LookupException le) {
>            throw new RuntimeException("Failed looking up connection
>from descriptor.");
>        }
>
>This however fails because the connection descriptor appears to return
>null for username and password...
>
>java.sql.SQLException: Invalid authorization specification,  message
>from server: "Access denied for user: '[EMAIL PROTECTED]'
>(Using password: NO)"
>        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1825)
>        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1752)
>
>Furthermore, it seems that OJB is NOT respecting my settings in the
>database.xml file completely. Here is the descriptor:
>
>    <jdbc-connection-descriptor
>           jcd-alias="fdbcon"
>           default-connection="true"         <-------------Notice this!
>           platform="MySQL"
>           jdbc-level="2.0"
>           driver="org.gjt.mm.mysql.Driver"
>           protocol="jdbc"
>           subprotocol="mysql"
>           dbalias="//localhost:3306/fdbtest2"
>           username="This is, of course, something else in the deployed
>file"
>           password="This is, of course, something else in the deployed
>file"
>        eager-release="false"
>           batch-mode="false"
>        useAutoCommit="1"
>        ignoreAutoCommitExceptions="false"
>     >
>
>        <connection-pool
>            maxActive="21"
>            validationQuery="" />
>
>        <sequence-manager
>className="org.apache.ojb.broker.util.sequence.SequenceManagerHighLowImpl">
>            <attribute attribute-name="grabSize" attribute-value="20"/>
>            <attribute attribute-name="autoNaming" attribute-value="true"/>
>            <attribute attribute-name="globalSequenceId"
>attribute-value="false"/>
>            <attribute attribute-name="globalSequenceStart"
>attribute-value="10000"/>
>        </sequence-manager>
>   </jdbc-connection-descriptor>
>
>HERE is the toString of the JdbcConnectionDescriptor:
>
>descriptor = [EMAIL PROTECTED]
>  jcd-alias=fdbcon
>  default-connection=false      <-------------Notice this!
>  dbms=MySQL
>  jdbc-level=2.0
>  driver=org.gjt.mm.mysql.Driver
>  protocol=jdbc
>  sub-protocol=mysql
>  db-alias=//localhost:3306/fdbtest2
>  user=<null>
>  password=*****
>  eager-release=false
>  ConnectionPoolDescriptor={testOnReturn=false, maxWait=5000,
>timeBetweenEvictionRunsMillis=-1, testOnBorrow=true,
>numTestsPerEvictionRun=10, maxActive=21, removeAbandonedTimeout=300,
>removeAbandoned=false, maxIdle=-1, testWhileIdle=false,
>whenExhaustedAction=0, logAbandoned=false,
>minEvictableIdleTimeMillis=600000}
>  batchMode=false
>  useAutoCommit=AUTO_COMMIT_SET_TRUE_AND_TEMPORARY_FALSE
>  ignoreAutoCommitExceptions=false
>
>[EMAIL PROTECTED]
>     sequenceManagerClass=class
>org.apache.ojb.broker.util.sequence.SequenceManagerHighLowImpl
>     Properties={grabSize=20, globalSequenceStart=10000,
>autoNaming=true, globalSequenceId=false}
>]
>
>
>Gus Heck wrote:
>
>> Ok I've been searching and grepping and pulling my hair out.... Where
>> does one find ConnectionDataSource() I am really hoping your answer is
>> not the obviously proprietary Orion application server... That seems
>> to be the only thing I get hits for though :( purchasing proprietary
>> server software _not_ an option for this project.
>>
>> Do you know of an alternate (open source) means of getting a
>> DataSource from a connection?
>>
>>> DDLExecutor exec = new DDLExecutor( new
>>> ConnectionDataSource(existing) );
>>> String sql = swriter.toString();
>>> exec.evaluateBatch( sql );
>>>
>>> John Marshall


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to