Spring configure for JDBC or Connection Pool

Spring using JDBC configuration

Spring的DB存取可以設定使用proxool pool或是jdbc driver manager的方式
DAO的設定部分都不用錯修改喔~~
下面是driver manager跟proxool的設定部分(需擇一,因為定義的bean id是一樣的啦,而且同一個專案,用一個就夠啦...)

[applicationContext.xml]
    <!-- Using JDBC Connection (select one with "Using proxool connection pool") -->
    <bean id="projectDataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName">
            <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
        </property>
        <property name="url">
            <value>
                jdbc:microsoft:sqlserver://server_ip:1433;DatabaseName=dbname
            </value>
        </property>
        <property name="username">
            <value>username</value>
        </property>
        <property name="password">
            <value>password</value>
        </property>
    </bean>

    <!-- Using proxool connection pool (select one with "Using JDBC Connection") -->
    <bean id="projectDataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource" destroy-method="close">
        <property name="alias" value="xxxxxPoolEvt" />
        <property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
        <property name="driverUrl" value="jdbc:sqlserver://server_ip:1433;databaseName=dbname" />
        <property name="user" value="username" /> 
        <property name="password" value="password" />
        <property name="maximumConnectionLifetime" value="180000" />
        <property name="maximumActiveTime" value="2500" />
        <property name="prototypeCount" value="5" />
        <property name="maximumConnectionCount" value="150" />
        <property name="minimumConnectionCount" value="10" />
        <property name="simultaneousBuildThrottle" value="10" />
        <property name="houseKeepingTestSql" value="select getdate()"/>
    </bean>   

    <!-- Session Factory -->
    <bean id="projectSessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="projectDataSource" />
        </property>
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.cache.provider_class">
                    org.hibernate.cache.EhCacheProvider
                </prop>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.SQLServerDialect
                </prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.cglib.use_reflection_optimizer">
                    false
                </prop>
            </props>
        </property>
    </bean>

    <!-- DAO configuration -->
    <bean id="XxxxDAO"
        class="com.test.daoImpl.XxxxDAOImpl">
        <property name="dataSource" ref="projectDataSource" />
    </bean>

[hibernate.cfg.xml]
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
        <mapping resource="ooo/aaa/bbb/xxx.hbm.xml"/>
        .... (list of hibernate table configures, skip it!)
    </session-factory>
</hibernate-configuration>

這個網誌中的熱門文章

Bash判斷參數是否存在

使用 minikube 輕鬆上手 kubernetes