Hibernate事务无法正常工作

时间:2012-05-18 14:06:09

标签: java hibernate transactions

我有一个非常奇怪的问题。我写了一个使用spring + hibernate的应用程序。我的hibernate配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="placeholderPrefix" value="${hospital_"/>
        <property name="location" value="hospital.properties"/>
    </bean>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass"                            value="${hospital_hibernate.driverClassName}"/>
        <property name="jdbcUrl"                                value="${hospital_hibernate.url}"/>
        <property name="user"                                   value="${hospital_hibernate.username}"/>
        <property name="password"                               value="${hospital_hibernate.password}"/>
        <!--
        <property name="acquireIncrement"                       value="20"/>
        <property name="acquireRetryAttempts"                   value="30"/>
        <property name="acquireRetryDelay"                      value="1000"/>
        <property name="autoCommitOnClose"                      value="true"/>
        <property name="checkoutTimeout"                    value="10000"/>
        <property name="debugUnreturnedConnectionStackTraces"   value="true"/>
        <property name="idleConnectionTestPeriod"               value="100"/>
        <property name="initialPoolSize"                        value="1"/>
        <property name="maxConnectionAge"                       value="1000"/>
        <property name="maxConnectionAge"                       value="1000000"/>
        <property name="maxIdleTime"                            value="200"/>
        <property name="maxIdleTimeExcessConnections"           value="3600"/>
        <property name="maxPoolSize"                            value="10"/>
        <property name="minPoolSize"                            value="1"/>
        <property name="preferredTestQuery"                     value="select 1"/>
        <property name="testConnectionOnCheckin"                value="false"/>
        <property name="unreturnedConnectionTimeout"            value="1000"/>
        -->
   </bean>

   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource"     ref="dataSource"/>
        <property name="schemaUpdate"   value="${hospital_schema.update}"/>
        <property name="mappingResources">
            <list>
                <value>com/saman/entity/hbms/Employee.hbm.xml</value>
                <value>com/saman/entity/hbms/Shift.hbm.xml</value>
                <value>com/saman/entity/hbms/Patient.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hospital_hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hospital_hibernate.showsql}</prop>
                <!--<prop key="hibernate.hbm2ddl.auto">update</prop>-->
                <prop key="hibernate.generate_statistics">true</prop>

                <!--<prop key="hibernate.cache.use_second_level_cache">true</prop>-->
                <!--<prop key="hibernate.cache.use_query_cache">true</prop>-->
                <!--<prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory</prop>-->
                <!--<prop key="net.sf.ehcache.configurationResourceName">ehcache.xml</prop>-->

                <prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
                <prop key="hibernate.use_sql_comments">true</prop>

                <prop key="hibernate.transaction.auto_close_session">true</prop>

            </props>
        </property>
        <property name="eventListeners">
            <map>
                <entry key="merge">
                    <bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>
                </entry>
            </map>
        </property>
    </bean>

    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
        <!--<property name="prepareConnection" value="true"/>-->
        <!--<property name="hibernateManagedSession" value="true"/>-->
    </bean>

</beans>

我也设置了我的tx:如下建议:

<tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
        <tx:method name="get*" propagation="REQUIRED" read-only="true"/>
        <tx:method name="*" rollback-for="java.lang.Throwable"/>
    </tx:attributes>
</tx:advice>


<aop:config>
    <aop:pointcut id="transactionServiceOperation"
                  expression="execution(* com.saman.svc.dalc.IEmployeeDA.*(..))"/>
    <aop:pointcut id="transactionServiceOperation2"
                  expression="execution(* com.saman.svc.dalc.IPatientDA.*(..))" />
    <aop:pointcut id="transactionServiceOperation3"
                  expression="execution(* com.saman.svc.dalc.IShiftDA.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionServiceOperation"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionServiceOperation2"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionServiceOperation3"/>
</aop:config>

这里的问题是,当我运行我的应用程序时,对于10或11个数据库访问它工作正常,但在那之后,似乎我的应用程序被锁定并且没有响应。

我真的不知道问题出在哪里。

P.S。我在patientDA,shiftDA和employeeDA类中定义了我的所有DA,这些类继承了IPatientDA,IShiftD和IEmployeeDA。

我的GenericDA:

public class GenericDA<TEntity, TId extends Serializable> extends HibernateDaoSupport implements IGenericDA<TEntity, TId>
{
    private Boolean isDateString = false;
    private Dialect mappingDialect = Dialect.mysql;

    public Boolean isDateString()
    {
        return isDateString;
    }

    public void setDateString(Boolean dateString)
    {
        isDateString = dateString == null ? false : dateString;
    }

    public Dialect getMappingDialect()
    {
        return mappingDialect;
    }

    public Dialect dialect()
    {
        return this.getMappingDialect();
    }

    public void setMappingDialect(Dialect mappingDialect)
    {
        this.mappingDialect = mappingDialect;
    }

    public Class getType()
    {
        Type tClass = null;
        Type type = this.getClass().getGenericSuperclass();
        if (type instanceof ParameterizedType)
        {
            ParameterizedType paramType = (ParameterizedType) type;
            tClass = paramType.getActualTypeArguments()[0];
        }
        return (Class) tClass;
    }

    public String getStringType()
    {
        return this.getType().getName();
    }

    public TEntity get(TId id)
    {
        return (TEntity) this.getSession().get(getType(), id);
    }

    public List<TEntity> get()
    {
        return (List<TEntity>) this.getSession().createCriteria(getType()).list();
    }

    public List<TEntity> get(long from, long size)
    {
        return this.getSession().createCriteria(getType())
                .setFirstResult((int) from)
                .setMaxResults((int) size)
                .list();
    }

    public TEntity insert(TEntity entity)
    {
        this.getSession().save(entity);
        return entity;
    }

    public TEntity update(TEntity entity)
    {
        try
        {
            this.getSession().evict(entity);
            this.getSession().update(entity);
            //this.getSession().flush();
        }
        catch (HibernateException ex)
        {
            this.getSession().evict(entity);
            this.getSession().clear();
//            this.getSession().update(entity);
            throw ex;
        }
        return entity;
    }

    public void delete(TId id)
    {
        this.getSession().delete(get(id));
    }

    public void delete(TEntity entity)
    {
        this.getSession().delete(entity);
    }

    public List<TEntity> get(int from, int size)
    {
        //noinspection unchecked
        return this.getSession().createCriteria(getType())
                .setFirstResult(from)
                .setMaxResults(size)
                .list();
    }

    public long getCount()
    {
        return (Long) this.getSession().createQuery("select count(entity.id) from " + getStringType() + " entity")
                .uniqueResult();
    }

    public Object getProperty(Long id, String property)
    {
        Criteria criteria = this.getSession().createCriteria(getType());
        criteria.add(Restrictions.eq("id", id));
        criteria.setProjection(Projections.property(property));
        criteria.setMaxResults(1);
        return criteria.uniqueResult();
    }

    public org.hibernate.Session prepareFilteredSession(Date effectiveDate)
    {
        org.hibernate.Session session = this.getSession();
        if (effectiveDate != null)
            session.enableFilter("effectiveDate").setParameter("fDate", effectiveDate);

        return session;
    }

    public void DisableFilter(org.hibernate.Session session)
    {
        session.disableFilter("effectiveDate");
    }

    public String getLikeExpression(String filter, String parameter)
    {
        String result = "LIKE";
        if (filter.startsWith("*"))
        {
            if (this.mappingDialect.equals(Dialect.oracle))
                result += " '%' ||";
            else
                result += " '%' +";
        }

        result += " :" + parameter + " ";
        if (filter.endsWith("*"))
        {
            if (this.mappingDialect.equals(Dialect.oracle))
                result += " || '%'";
            else
                result += " + '%'";
        }
        return result;
    }

    public String getLikeParameter(String parameter)
    {
        if (parameter.startsWith("*"))
            parameter = parameter.substring(1);

        if (parameter.endsWith("*"))
            parameter = parameter.substring(0, parameter.length() - 1);

        return parameter;
    }

    public Query setDateParameter(Query query, String name, Date value)
    {
        if (this.isDateString)
            return query.setString(name, String.format("%1$tY-%1$tm-%1$td %tT", value));
        else
            return query.setParameter(name, value);

    }
}

例如我的ShiftDA示例是:

public class ShiftDA extends GenericDA<ShiftEntity, Long> implements IShiftDA {

    public void updateNumberOfPatients( int numberOfPatients, Long columnID ){
        try{
            Statement st = this.getSession().connection().createStatement();
            String sql = "UPDATE shift SET CurrentNumberOfPatients='" + numberOfPatients + "' WHERE ShiftId='" + columnID.toString() + "' ";
            st.executeUpdate(sql);

        }catch( Exception e ){
            e.printStackTrace();
        }

    }

}

1 个答案:

答案 0 :(得分:1)

根据这些C3P0设置(min = 1 max = 10)以及您指定的10或11个请求,我会猜测并说您正在耗尽连接池,因为某些东西没有释放它们。我怀疑这与tx-manager有关,并且不知道会话的生命何时结束。

另外 - 我会使用公共DBCP而不是C3P0。

你能发布DA对象在做什么吗?