无法使用Spring 4和Hibernate 4创建与Oracle 11g的连接

时间:2015-03-13 17:41:53

标签: java spring oracle hibernate oracle11g

我的应用程序使用的是Spring 4,Hibernate 4,Java 1.7。这是桌面应用程序而不是Web应用程序,因此不涉及服务器。我正在尝试创建与数据库的连接。为了简洁起见 - 我无法复制和粘贴 - 我遗漏了一些非必要的东西来解决这个问题。这是我的春季配置:

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="oracle.jdbc.OracleDriver" />
    <property name="jdbcUrl" value="jdbc:oracle:thin:@dbServer:1521:service" />
    <property name="properties">
        <props>
            <prop key="user">user</prop>
            <prop key="password">password</prop>
        </props>
    </property>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.help.please" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.connection.autocommit">true</prop>
        </props>
    </property>

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

我已将sessionFactory自动装入DAO。当我尝试从sessionFactory获取连接时,日志看起来像是这样(再次缩短):

INFO [main] C3P0Registry:216 Initializing c3p0-0.9.2.1 [built 20-March-2013 10:47:27 +0000; debug? true; trace: 10]
INFO [main] Version:66 - HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
INFO [main] Version:54 - HHH000412: Hibernate Core {4.3.8.Final}
INFO [main] Environment:224 - HHH000205: Loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=org.h2.Driver, hibernate.service.allow_crawling=false, hibernate.dialect=org.hibernate.dialect.H2Dialect, hibernate.max_fetch_depth=5, hibernate.format_sql=true, hibernate.generate_statistics=true, hibernate.connection.username=sa, hibernate.connection.url=jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE, hibernate.bytecode.use_reflection_optimizer=false, hibernate.jdbc.batch_versioned_data=true, hibernate.connection.pool_size=5}
INFO [main] Environment:346 - HHH000021: Bytecode provider name : javassist
INFO [main] AbstractPoolBackedDataSource:522 - Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailer -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> [mumbo jumbo], debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> oracle.jdbc.OracleDriver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> [mumbo jumbo], idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:oracle:thin:@dbServer:1521:service, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> maxPoolSize -> 15, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, preferredTestQuery -> null, properties -> {user=****, password=********}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, userOverrides -> {}, usesTraditionalReflectiveProxies -> false ]
WARN [main] JdbcServicesImpl:204 - HHH000342: Could not obtain connection to query metadata : ORA-01017: invalid username/password; logon denied

我成功地使用用户使用其他应用程序以及SQL Developer登录数据库。 我已多次确认用户/密码是否正确。我已尝试同时使用ojdbc6.jarojdbc7.jar,希望它可能就是这样。我做了一些研究,发现可能是用户名/密码在被发送到Oracle之前可能会被自动大写,但我知道知道验证的方法。

可能很重要的一点是,当我使用Spring 3和Hibernate 3时,这段代码正好连接到数据库。有关如何解决这个问题的建议吗?

修改

重要的是要注意我正在使用注释和xml的组合来配置我的项目。我正在使用@Autowired来注入我的服务,daos和会话工厂。我在我的dao图层中使用@Transactional@Repository。我不知道这是否有所作为,但请看下面的答案,看看我是如何运作的。

1 个答案:

答案 0 :(得分:1)

我发现我需要在{spring hibernate.cfg.xml bean中添加sessionFactory文件。

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.url">jdbc:oracle:thin:@dbServer:1521:service</property>
        <property name="connection.username">user</property>
        <property name="connection.password">password</property>
        <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
        <property name="hibernate.connection.autocommit">true</property>
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
        <property name="hibernate.show_sql">true</property>
    </session-factory>
</hibernate-configuration>

现在我的弹簧配置改为:

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="oracle.jdbc.OracleDriver" />
    <property name="jdbcUrl" value="jdbc:oracle:thin:@dbServer:1521:service" />
    <property name="user" value="user" />
    <property name="password" value="password" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.help.please" />
    <property name="configLocation">
        <value>classpath:com.help.please/hibernate.cfg.xml</value>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

我不太清楚为什么会这样做的解释,但我认为的方式向我解释的是,属性并没有全部传递给Hibernate所以像这样配置Hibernate和Spring一样可以帮助它获得所需的正确配置。如果我错了,请随时纠正我。