Hibernate 动态刷新entitymanager工厂

Hibernate 动态刷新entitymanager工厂,hibernate,spring,jpa,Hibernate,Spring,Jpa,我的application-datasource.xmls在下面。我的问题是我不想在这里提供用户名和密码 上下文文件我想在用户登录时在运行时获取用户名和密码,但我不知道如何获取 这样做 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLS

我的application-datasource.xmls在下面。我的问题是我不想在这里提供用户名和密码 上下文文件我想在用户登录时在运行时获取用户名和密码,但我不知道如何获取 这样做

    <?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,以便它可以使用新连接创建实体对象。在我当前的场景中,如果出现错误,则在获取数据时无法打开连接。
谢谢

整个ManagerFactory是全球性的,因此您的建议不会很有效。如果刷新(即重新创建)EnitytManagerFactory,将影响整个应用程序,而不仅仅是当前请求/线程/用户。如果您真的希望每个用户拥有不同的数据库凭据,那么Hibernate不是您的最佳选择。实际上,您必须为每个会话创建一个唯一的EntityManagerFactory,这将产生大量的性能开销和内存复制。您也不能使用连接池,因为您仍然需要为每个用户打开一个新连接

我的建议是,重新思考您的需求和体系结构。如果您无法摆脱数据库中的用户凭据,并且必须使用Hibernate,那么在需要为每个用户会话打开和关闭新的EntityManagerFactory时,您将不得不在性能、内存和复杂性方面受到重大影响。

可以参考entity manager factory的更多信息。