有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java MYSQL JPA连接提供错误的用户名和密码

我试图用Ejb在JPA中同时连接Postgres和mysql,以下是我的持久性。xml和我正在wildfly 10中部署代码

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">




    <persistence-unit name="pl4smsMYSQL-persistence">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>


        <!-- <shared-cache-element>ENABLE_SELECTIVE</shared-cache-element> -->
        <properties>
  <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/newtable" />
    <property name="javax.persistence.jdbc.user" value="root" />
    <property name="javax.persistence.jdbc.password" value="root" />
    <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
    <property name="hibernate.show_sql" value="true" />
    <property name="hibernate.format_sql" value="true" />
    <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
    <property name="hibernate.hbm2ddl.auto" value="validate" />
    <!-- Configuring Connection Pool -->
    <property name="hibernate.c3p0.min_size" value="5" />
    <property name="hibernate.c3p0.max_size" value="20" />
    <property name="hibernate.c3p0.timeout" value="500" />
    <property name="hibernate.c3p0.max_statements" value="50" />
    <property name="hibernate.c3p0.idle_test_period" value="2000" />
<!-- <property name="hibernate.hbm2ddl.auto" value="create"/> -->


        <!--    <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/newtable" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.connection.username" value="root" />
            <property name="hibernate.connection.password" value="root" />
            <property name="hibernate.listeners.envers.autoRegister"
                value="false" />

            <property name="hibernate.id.new_generator_mappings" value="true" />
            <property name="hibernate.cache.use_second_level_cache"
                value="false" />
               <property name="show_sql" value="true"/>
            <property name="hibernate.c3p0.min_size" value="50" /> <property 
                name="hibernate.c3p0.max_size" value="100" /> <property name="hibernate.c3p0.timeout" 
                value="100" /> <property name="hibernate.c3p0.idle_test_period" value="1" 
                /> <property name="hibernate.cache.use_query_cache" value="false" /> <property 
                name="javax.persistence.sharedCache.mode" value="ENABLE_SELECTIVE" /> <property 
                name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory" 
                /> <property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.SingletonEhCacheProvider" 
                /> <property name="hibernate.cache.use_query_cache" value="true" /> <property 
                name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory" 
                /> <property name="net.sf.ehcache.configurationResourceName" value="/ehcache.xml" 
                />
            <property name="hibernate.enable_lazy_load_no_trans" value="true" />
            uncomment generate_statistics line to generate cache statistics
            <property name="hibernate.generate_statistics" value="true" />
            <property name="eclipselink.jdbc.batch-writing" value="JDBC" /> <property 
                name="eclipselink.jdbc.batch-writing.size" value="20" />
            <property name="hibernate.hbm2ddl.auto" value="create"/> -->
        </properties>
    </persistence-unit>
        <persistence-unit name="pl4sms-persistence">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>


    <!--    <shared-cache-element>ENABLE_SELECTIVE</shared-cache-element> -->
        <properties>




            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
            <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/pl4smstest" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.connection.username" value="root" />
            <property name="hibernate.connection.password" value="password" />
            <property name="hibernate.listeners.envers.autoRegister"
                value="false" />

            <property name="hibernate.id.new_generator_mappings" value="true" />
            <property name="hibernate.cache.use_second_level_cache"
                value="false" />
            <!-- <property name="hibernate.c3p0.min_size" value="50" /> <property 
                name="hibernate.c3p0.max_size" value="100" /> <property name="hibernate.c3p0.timeout" 
                value="100" /> <property name="hibernate.c3p0.idle_test_period" value="1" 
                /> <property name="hibernate.cache.use_query_cache" value="false" /> <property 
                name="javax.persistence.sharedCache.mode" value="ENABLE_SELECTIVE" /> <property 
                name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory" 
                /> <property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.SingletonEhCacheProvider" 
                /> <property name="hibernate.cache.use_query_cache" value="true" /> <property 
                name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory" 
                /> <property name="net.sf.ehcache.configurationResourceName" value="/ehcache.xml" 
                />
            <property name="hibernate.enable_lazy_load_no_trans" value="true" />
            uncomment generate_statistics line to generate cache statistics
            <property name="hibernate.generate_statistics" value="true" />
            <property name="eclipselink.jdbc.batch-writing" value="JDBC" /> <property 
                name="eclipselink.jdbc.batch-writing.size" value="20" />
            <property name="hibernate.hbm2ddl.auto" value="create"/> -->
        </properties>
    </persistence-unit>




</persistence>

这是pom。我正在使用的xml

<dependency>
            <groupId>org.apache.openejb</groupId>
            <artifactId>tomee-embedded</artifactId>
            <version>1.0.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
        </dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.2.3.Final</version>
</dependency>

我可以连接到postgres,但对于mysql,它给出了错误的用户名和密码错误,我尝试使用commad提示符连接mysql,它使用相同的用户名和密码连接。我尝试删除Postgres连接,并仅使用mysql连接,但仍然会出现相同的错误。 下面是我犯的错误

    Throwable while attempting to get a new connection: null: javax.resource.ResourceException: IJ031084: Unable to create connection
    at org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.createLocalManagedConnection(LocalManagedConnectionFactory.java:336)
    at org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.getLocalManagedConnection(LocalManagedConnectionFactory.java:343)
    at org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.createManagedConnection(LocalManagedConnectionFactory.java:278)
    at org.jboss.jca.core.connectionmanager.pool.mcp.SemaphoreArrayListManagedConnectionPool.createConnectionEventListener(SemaphoreArrayListManagedConnectionPool.java:1289)
    at org.jboss.jca.core.connectionmanager.pool.mcp.SemaphoreArrayListManagedConnectionPool.getConnection(SemaphoreArrayListManagedConnectionPool.java:492)
    at org.jboss.jca.core.connectionmanager.pool.AbstractPool.getSimpleConnection(AbstractPool.java:627)
    at org.jboss.jca.core.connectionmanager.pool.AbstractPool.getConnection(AbstractPool.java:599)
    at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.getManagedConnection(AbstractConnectionManager.java:579)
    at org.jboss.jca.core.connectionmanager.tx.TxConnectionManagerImpl.getManagedConnection(TxConnectionManagerImpl.java:430)
    at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.allocateConnection(AbstractConnectionManager.java:737)
    at org.jboss.jca.adapters.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:162)
    at org.jboss.as.connector.subsystems.datasources.WildFlyDataSource.getConnection(WildFlyDataSource.java:74)
    at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:139)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcServicesImpl.java:279)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:124)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1887)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:857)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:425)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:849)
    at org.jboss.as.jpa.hibernate4.TwoPhaseBootstrapImpl.build(TwoPhaseBootstrapImpl.java:44)
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:154)
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:117)
    at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:665)
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1.run(PersistenceUnitServiceImpl.java:182)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    at org.jboss.threads.JBossThread.run(JBossThread.java:320)
Caused by: org.h2.jdbc.JdbcSQLException: Wrong user name or password [28000-173]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:331)
    at org.h2.message.DbException.get(DbException.java:171)
    at org.h2.message.DbException.get(DbException.java:148)
    at org.h2.message.DbException.get(DbException.java:137)
    at org.h2.engine.Engine.validateUserAndPassword(Engine.java:302)
    at org.h2.engine.Engine.createSessionAndValidate(Engine.java:147)
    at org.h2.engine.Engine.createSession(Engine.java:122)
    at org.h2.engine.Engine.createSession(Engine.java:28)
    at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:313)
    at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:105)
    at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:90)
    at org.h2.Driver.connect(Driver.java:73)
    at org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.createLocalManagedConnection(LocalManagedConnectionFactory.java:312)
    ... 32 more

共 (1) 个答案

  1. # 1 楼答案

    更好的办法是在WildFly中create two datasources,然后在持久性中引用它们。xml文件:

    <persistence-unit name="pl4smsMYSQL-persistence" transaction-type="JTA">
         <jta-data-source>java:jboss/datasources/MySqlDS</jta-data-source>
         ...
    </persistence-unit>
    <persistence-unit name="pl4sms-persistence" transaction-type="JTA">
         <jta-data-source>java:jboss/datasources/PostgreSqlDS</jta-data-source>
         ...
    </persistence-unit>
    

    大多数配置属性都会变得多余

    这还有一个好处,就是您的服务器将针对每个环境进行独立配置,因此您不需要为测试和生产构建不同的应用程序

    请注意,然后还可以从应用程序中删除驱动程序JAR,因为它们是单独部署到服务器的