【发布时间】:2015-03-09 22:57:42
【问题描述】:
下面是来自 Hibernate.xml 的 Hibernate 配置
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.c3p0.timeout">300</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
.....
</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
下面是 GenricDaoImpl 类的代码(代码 1)
@Override
public T save(T t) {
Session session = getSessionFactory().openSession();
Transaction tx=session.beginTransaction();
session.saveOrUpdate(t);
tx.commit();
session.close();
return t;
}
以及项目中的其他代码(代码 2)
Query executeQuery = getSession().createQuery(hql);
UserProfileuser = (UserProfile) executeQuery.uniqueResult();
在我在项目中使用的两个代码之上。我的问题是需要遵循哪些编码?代码 1 或代码 2 以避免最大连接错误。?我最多可以连接 1000 个与数据库的连接。但在某些情况下,它会超过 1000 。所以我想保持最少的数据库连接。 请指导我。
【问题讨论】:
标签: java mysql spring hibernate c3p0