Spring MVC + ComboPooledDataSource(休眠)

时间:2011-11-20 00:49:33

标签: mysql hibernate spring-mvc

这是我第一次在hibernate中使用ComboPooledDataSource,但我认为我的配置有问题,所以当我调用DAO从数据库中检索所有数据时,这不会给我任何回报。

GenView

@RequestMapping(value="/getHospitals.ajax")
public @ResponseBody Map<String,? extends Object> loadHospitals(){

    HashMap<String, List<Hastaneler>> modelMap = new HashMap<String,List<Hastaneler>>();
modelMap.put("hastaneler", genBUS.getHospitals());

return modelMap;    
}

GenBUS

@Service
@Transactional(isolation = Isolation.DEFAULT, readOnly = false, propagation = Propagation.REQUIRED)
public class GenBUS implements IGenBUS {

    private Log log = LogFactory.getLog(GenBUS.class);

    @Autowired
    private SessionClientData   scd;

    @Autowired
    private GenDAO genDAO;

    private JdbcTemplate jdbcTemplate;

    @Autowired
    private ComboPooledDataSource comboPooledDataSource;

    @PostConstruct
    public void dataSource2JdbcTemplate() {
        this.jdbcTemplate = new JdbcTemplate(comboPooledDataSource);
    }

    public GenDAO getGenDAO() {
        return genDAO;
    }

        @Override
    public List<User> getHospitals() {
        return genDAO.loadAllObject(User.class);
    }

GenDAO

@Repository
public class GenDAO extends BaseDAO{

    private Log log = LogFactory.getLog(GenDAO.class);


    @Autowired
    private SessionClientData scd;

    @Autowired
    private ComboPooledDataSource comboPooledDataSource;

    private JdbcTemplate jdbcTemplate;

    @PostConstruct
    public void dataSource2JdbcTemplate() {
        this.jdbcTemplate = new JdbcTemplate(comboPooledDataSource);
    }


    @Autowired
    public GenDAO(SessionFactory sessionFactory) {
        logger.debug("GenDAO constructor is called !!!!!!!!");
        System.out.println("genDaoooooooooooooooo.....");
        setSessionFactory(sessionFactory);
    }

BaseDAO

public class BaseDAO extends HibernateDaoSupport {

    protected org.hibernate.impl.SessionFactoryImpl baseSessionFactory;

        public<T> List<T>  loadAllObject(Class<T> clazz) {

        return (List<T>) getHibernateTemplate().loadAll(clazz);
    }

dao.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

    <tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" />

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="baseSessionFactory"/>
    </bean>

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
        <property name="location" value="classpath:/resources/test.properties"/>
    </bean>

    <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="${database.driverClass}"/>
        <property name="jdbcUrl" value="${database.url}"/>      

        <property name="properties">
            <props>
                <prop key="user">${database.user}</prop>
                <prop key="password">${database.password}</prop>
            </props>
        </property>

        <!--<property name="user" value="${database.user}"/>
        <property name="password" value="${database.password}"/> -->

        <property name="maxPoolSize" value="50"/>
        <property name="initialPoolSize" value="2"/>
        <property name="minPoolSize" value="1"/>
        <property name="maxStatements" value="200"/>
        <property name="maxIdleTime" value="300"/>
        <property name="acquireIncrement"  value="10"/>
        <property name="unreturnedConnectionTimeout" value="90"/>
        <property name="maxConnectionAge" value="120"/>
    </bean>



    <bean id="baseSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="comboPooledDataSource"/>
    <!--    <property name="entityInterceptor">
            <bean class="generic.logging.AuditTrailInterceptor"/>
        </property> -->
        <property name="packagesToScan">
            <list>
                <value>model.GenUser</value>
                <value>model.Hastaneler</value> 
                <value>model.Hastanelerim</value>
                <value>model.User</value>
             </list>
        </property>
        <property name="cacheProvider" ref="ehCacheProvider"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop> 
                <prop key="hibernate.max_fetch_depth">2</prop>
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.dialect">${database.dialect}</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.use_sql_comments">true</prop>
                <prop key="hibernate.format_sql">false</prop>
                <prop key="hibernate.generate_statistics">true</prop>
                <prop key="current_session_context_class">thread</prop>
                <!--<prop key="hibernate.hbm2ddl.auto">none</prop>-->
            </props>
        </property>
        <!--  <property name="eventListeners">
            <map>
                <entry key="pre-insert"><bean class="generic.logging.HibernateAuditLogListener"/></entry>
                <entry key="pre-delete"><bean class="generic.logging.HibernateAuditLogListener"/></entry>
                <entry key="pre-update"><bean class="generic.logging.HibernateAuditLogListener"/></entry>
         </map>
        </property>-->
    </bean>


     <bean id="ehCacheProvider" class="org.hibernate.cache.EhCacheProvider"/>
</beans>

test.properties

database.url=jdbc:mysql://127.0.0.1:3306/acilservis
database.driverClass=com.mysql.jdbc.Driver
database.user=root
database.password=
database.dialect=org.hibernate.dialect.MySQLDialect
format_sql=true
show_sql=true

我使用Mysql InnoDB作为数据库引擎......有什么建议吗?

1 个答案:

答案 0 :(得分:3)

我已经通过更改jdbcTemplate解决了这个问题...我重新配置它以使用我的sessionFactory,这有效:)