2012-02-24 75 views
1

我的application-datasource.xmls在我的問題之下是我不想在這個 上下文文件中提供用戶名和密碼我想在運行時獲取用戶名和密碼時用戶登錄但我不知道如何 來做到這一點。立即刷新entitymanager工廠

<?xml version="1.0" encoding="UTF-8"?> 
    <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:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-2.0.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schem...ng-aop-2.0.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> 

    <tx:annotation-driven /> 
    <!-- DataSource has been Lazily initialized, set lazy-init to false in production --> 

    <bean id="datasource" 
    class="com.mchange.v2.c3p0.ComboPooledDataSource" lazy-init="true" destroy-method="close"> 
    <!-- Tracker created for connection problem: https://sourceforge.net/tracker/?func=detail&aid=3176467&group_id=25357&atid=38369 0--> 
    <property name="driverClass" value="com.ibm.as400.access.AS400JDBCDriver" /> 
    <property name="jdbcUrl" value="${url}" /> 
    <!--<property name="user" value="${username}" /> 
    <property name="password" value="${password}" />--> 

    <!-- Pool Size Properties follow --> 
    <property name="minPoolSize" value="0"/> <!-- Minimum no. of pooled connections --> 
    <property name="initialPoolSize" value="1" /> <!-- Initial no. of pooled connections (>minimum)[optional] --> 
    <property name="maxPoolSize" value="2"/> <!-- Maximum no. of pooled connections (>minimum) --> 
    <property name="acquireIncrement" value="1"/> <!-- Connections to be added every time the need arises --> 

    <!-- Connection Establishment Strategy follows --> 
    <property name="acquireRetryAttempts" value="5" /> <!-- Retry Attempts on Database connection failure --> 
    <property name="acquireRetryDelay" value="1000"/> <!-- Milliseconds between re-tries --> 
    <property name="breakAfterAcquireFailure" value="true" /> <!-- Aggressively break DataSource on connection failure --> 

    <!-- Prepared Statement pooling --> 
    <property name="maxStatements" value="300"/> <!-- Value ~= maxPoolSize * no. of (frequently used)stored procedures --> 
    <property name= "maxStatementsPerConnection" value="15" /> <!-- Statement caching per connection for improved performance --> 

    <!-- Connection Age related settings --> 
    <property name="maxIdleTime" value="300" /> <!-- Seconds for a connection to remain idle before being closed --> 
    <property name="unreturnedConnectionTimeout" value="300" /> <!-- Wait for number of seconds for application to release a connection --> 

    <property name="idleConnectionTestPeriod" value="30000"/> <!-- Test for idle connections(In milliseconds) --> 
    <property name= "autoCommitOnClose" value="true" /> <!-- For ensuring all transactions to commit/rollback on close --> 
    <property name="debugUnreturnedConnectionStackTraces" value ="true" /> 
    <!-- Ignored overrides --> 

    <!-- Used for connection testing during startup --> 
    <!--property name="testConnectionOnCheckin" value="true" /--> <!-- Test for connection validity asynchronously --> 

    <!--property name="initialPoolSize" value="1" /--> <!-- Initial no. of pooled connections (>minimum)[optional] --> 
    <!--property name="idleConnectionTestPeriod" value="30000" /> In milliseconds(Overridden) --> 
    <!--property name="maxConnectionAge" value="1800" /--> <!-- Life in seconds for any connection(busy/idle) before being deleted --> 
    <!--property name="numHelperThreads" value="3" /--> <!-- Perform JDBC operations asynchronously --> 

    <bean id="entityManagerFactory" 
    class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean"> 
    <property name="persistenceUnitName" value="persistence" /> 
    <property name="persistenceXmlLocation" value="classpathersistence.xml" /> 
    <property name="dataSource" ref="datasource" /> 
    <property name="loadTimeWeaver"> 
    <bean class="org.springframework.instrument.classloading .InstrumentationLoadTimeWeaver" /> 
    </property> 
    <property name="jpaVendorAdapter" ref="vendorAdapter" /> 
    </bean> 

    <bean id="vendorAdapter" 
    class="org.springframework.orm.jpa.vendor.Hibernat eJpaVendorAdapter"> 
    <property name="databasePlatform" value="${dialect}" /> 
    <property name="showSql" value="${show_sql}" /> 
    <property name="generateDdl" value="false" /> 
    </bean> 

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionM anager"> 
<property name="entityManagerFactory" ref="entityManagerFactory" /> 
</bean> 

<bean class="org.springframework.orm.jpa.support.Persist enceAnnotationBeanPostProcessor" /> 
<bean class="org.springframework.dao.annotation.Persiste nceExceptionTranslationPostProcessor"/> 
</beans> 

一些網站建議使用UserCredintialDataSource,但它不工作實際上我想刷新的EntityManagerFactory因此它可以用在我目前的情況下,新的連接,同時獲取它給人的錯誤無法打開連接使得實體對象數據。 謝謝

+0

有關實體管理器工廠的更多信息,請參閱http://stackoverflow.com/a/4544053/366964。 – 2012-02-24 13:00:30

回答

2

該EntityManagerFactory是全球性的,所以你提出的將不會工作得很好。如果刷新(即重新創建)EnitytManagerFactory,那將影響整個應用程序,而不僅僅是當前的請求/線程/用戶。如果你真的想爲每個用戶擁有不同的數據庫證書,Hibernate並不是你的最佳選擇。你基本上必須爲每個會話創建一個唯一的EntityManagerFactory,這會產生大量的性能開銷和內存重複。你也不能使用連接池,因爲你需要爲每個用戶打開一個新的連接。

我的建議,重新考慮你的要求和你的架構。如果您無法擺脫數據庫中的用戶憑證,並且必須使用Hibernate,那麼您需要爲每個用戶會話打開和關閉新的實體管理員因素,因此必須在性能,內存和複雜性方面取得顯着成效。