Initialization problem

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Initialization problem

Gelzhaeuser
Hi all.

I have a little problem over here which I somehow couldn't solve with
the help of the documentation:
I have my mappers defined in various xml files (the statements are
quite complex) and a javax.sql.Datasource Object which is the
datasource I need to use.How do I create a SQLSessionFactory from
that?

From my understanding, I can use either a reader in order to use the
xml files, which doesn't allow me to use the datasource, or a
configuration object which doesn't allow me to read the xml files.

Can anyone shed a bit of light onto this?

Thanks.

Thomas.
Reply | Threaded
Open this post in threaded view
|

Re: Initialization problem

François Schiettecatte
Thomas

More answer than you want I am sure, but I thought this could be useful to others on the list, and I am happy to share code if people want.

When you say 'javax.sql.Datasource' I think you mean the POOLED data source which is part of MyBatis, in which case you need to do this:

- Add the following data source to the MyBatis xml config file (I use mysql so your driver class may be different):

                <!-- Development configuration -->
                <environment id="development">
                       
                        <transactionManager type="JDBC">
                                <property name="" value="" />
                        </transactionManager>
                       
                        <!-- POOLED data source -->
                        <dataSource type="POOLED">

                                <!-- Basic configuration -->
                                <property name="driver" value="com.mysql.jdbc.Driver" />
                                <property name="url" value="${url}" />
                                <property name="username" value="${username}" />
                                <property name="password" value="${password}" />
                               
                                <!-- Basic pool configuration -->
<!--
                                <property name="poolMaximumActiveConnections" value="10" />
                                <property name="poolMaximumIdleConnections" value="5" />
                                <property name="poolMaximumCheckoutTime" value="20000" />
                                <property name="poolTimeToWait" value="20000" />
                                <property name="poolPingQuery" value="" />
                                <property name="poolPingEnabled" value="false" />
                                <property name="poolPingConnectionsNotUsedFor" value="0" />
-->

                        </dataSource>

                </environment>


- Create the sql session factory as follows:

                        Reader reader = Resources.getUrlAsReader("file:/home/poplar/conf/mybatis-config.xml");

                        // Create the properties and populate it with the values which
                        // will be substituted in the sqlmap-configuration.xml file
                        // http://dev.mysql.com/doc/refman/5.1/en/connector-j-reference-configuration-properties.html
                        final Properties properties = new Properties();
                        final String url = "jdbc:mysql://" + hostName + "/" + databaseName + "?" + MyBatis.JDBC_MYSQL_URL_QUERY_OPTIONS;
                        properties.setProperty("url", url);
                        properties.setProperty("username", userName);
                        properties.setProperty("password", password);

                        // Create the sql session factory
                        final SqlSessionFactory sqlSessionFactory = MyBatis.sqlSessionFactoryBuilder.build(reader, properties);

- Note that MyBatis picks up the properties from the xml config file and passes them to the setProperties() method in your data source factory.



And here is more that you wanted to know, which is if you want to use another pool such as C3P0:

- You will need to create a data source factory to create data sources for iBatis which will look something like this (this is for the C3P0 data source but the pattern is the same I would guess):

// Package location
package com.poplar.sql;

// Import Javax stuff
import javax.sql.DataSource;


// Import iBatis stuff
import org.apache.ibatis.datasource.DataSourceFactory;


// Import C3P0 stuff
import com.mchange.v2.c3p0.ComboPooledDataSource;


public class C3P0DataSourceFactory implements DataSourceFactory {

        private ComboPooledDataSource dataSource = null;

        public C3P0DataSourceFactory () {
                ;
        }

        public void setProperties(Properties properties) {
                this.dataSource = new ComboPooledDataSource();
        }

        public DataSource getDataSource() {
                return (this.dataSource);
        }
}


- It turn out that setProperties() is called before getDataSource() which is why the data source is created in setProperties() in the example above.

- You will need to add the data source class to the myBatis.xml config file, like this:

                <!-- Production configuration -->
                <environment id="production">
               
                        <transactionManager type="JDBC">
                                <property name="" value="" />
                        </transactionManager>

                        <!-- C3P0 data source, see: http://www.mchange.com/projects/c3p0/ for settings -->
                        <dataSource type="com.poplar.sql.C3P0DataSourceFactory">

                                <!-- Basic configuration -->
                                <property name="driver" value="com.mysql.jdbc.Driver" />
                                <property name="url" value="${url}" />
                                <property name="username" value="${username}" />
                                <property name="password" value="${password}" />



- And you create the sql session factory as above.


Cheers

François



On Sep 7, 2010, at 5:18 AM, Gelzhaeuser wrote:

> Hi all.
>
> I have a little problem over here which I somehow couldn't solve with
> the help of the documentation:
> I have my mappers defined in various xml files (the statements are
> quite complex) and a javax.sql.Datasource Object which is the
> datasource I need to use.How do I create a SQLSessionFactory from
> that?
>
> From my understanding, I can use either a reader in order to use the
> xml files, which doesn't allow me to use the datasource, or a
> configuration object which doesn't allow me to read the xml files.
>
> Can anyone shed a bit of light onto this?
>
> Thanks.
>
> Thomas.