[英]Single database connection with HibernateTemplate and SessionFactory
在java方面,一切正常,但當我查看V $ SESSION特殊oracle表時,在我的日志表中記錄任何登錄或注銷操作,這是一場災難......每個查詢都進行登錄/注銷操作。 所以這是我的問題:有沒有辦法將Spring配置為與數據庫建立唯一連接,或者我連接的方式有什么問題? 在這里,我的數據源bean配置:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>oracle.jdbc.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@nanssunorad:1523:nanorad3</value>
</property>
<property name="username">
<value>foo</value>
</property>
<property name="password">
<value>bar</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>hibernateESign.cfg.xml</value>
</property>
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="IXalVaParametresDAO" class="fr.asterion.archivage.hibernate.XalVaParametresDAO">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
在我的應用程序中,為了獲取數據庫參數的DAO,我做了一個
IXalVaParametresDAO parametreDAO = (IXalVaParametresDAO) ConfigApplication
.getApplicationContext(this.log).getBean("IXalVaParametresDAO");
最后,在我的DAO課程中,我做了這樣的事情:
public class XalVaParametresDAO implements IXalVaParametresDAO
{
private HibernateTemplate hibernateTemplate;
public void setSessionFactory(SessionFactory sessionFactory)
{
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}
public List<XalVaParametres> findAll()
{
log.debug("finding all XalVaParametres instances");
try
{
String queryString = "from XalVaParametres";
List lst = this.hibernateTemplate.find(queryString);
在我的應用程序中,每次調用“查找”方法時,它都會進行數據庫登錄/注銷。 我猜這個問題在另一個DB上是一樣的。 我做對了嗎? 我猜不會。 我希望HibernateTemplate能夠一勞永逸地創建和保持會話。 那可能嗎 ? 謝謝你的想法
Manux
不建議在生產中使用DriverManagerDataSource
,因為它不執行連接池。
您需要使用連接池,例如c3p0或Apache DBCP 。
最好使用應用程序服務器的連接池並在spring xml文件中指定jndi:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/DSTest"/>
</bean>
您可以使用具有連接緩存的本機oracle數據源,如:
<bean id="myDataSource" class="oracle.jdbc.pool.OracleDataSource" destroy-method="close">
<property name="connectionCachingEnabled" value="true"/>
<property name="URL">
<value>${jdbc.url}</value>
</property>
<property name="user">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
<property name="connectionCacheProperties">
<value>
MinLimit:1
MaxLimit:1
InitialLimit:1
ConnectionWaitTimeout:120
InactivityTimeout:180
ValidateConnection:true
MaxStatementsLimit:0
</value>
</property>
</bean>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.