2014-05-24 146 views
-3

我在嘗試運行我的代碼時遇到此錯誤。錯誤Hibernate Spring:連接被拒絕

我的春天配置類:

<context:component-scan base-package="com.math"/> 

<mvc:annotation-driven /> 
<mvc:default-servlet-handler /> 
<context:annotation-config /> 

<mvc:resources mapping="/resources/**" location="/resources/" /> 
<mvc:resources mapping="/game/**" location="/game/" /> 

<import resource="classpath:*.xml"/> 

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/pages/"/> 
    <property name="suffix" value=".jsp"/> 
    <property name="contentType" value="text/html;charset=utf-8" /> 
</bean> 

我的Hibernate配置XML:

<hibernate-configuration> 
<session-factory> 
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="connection.url">jdbc:mysql://localhost:3306/mathgame</property> 
    <property name="connection.username">root</property> 
    <property name="connection.password">root</property> 

    <property name="connection.pool_size">5</property> 

    <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 

    <property name="current_session_context_class">thread</property> 

    <!--property name="show_sql">true</property> 
    <property name="format_sql">true</property--> 

    <property name="hbm2ddl.auto">create</property> 

    <mapping class="com.math.pojo.EntityUser"></mapping> 
    <mapping class="com.math.pojo.EntityAnswer"></mapping> 
    <mapping class="com.math.pojo.EntityGame"></mapping> 
</session-factory> 
</hibernate-configuration> 

而且我的堅持的xml:

<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
    <property name="url" value="jdbc:mysql://localhost:3306/mathgame" /> 
    <property name="username" value="root" /> 
    <property name="password" value="root" /> 
</bean> 

<bean id="mySessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="myDataSource" /> 
    <property name="packagesToScan" value="com.math.pojo" /> 
    <property name="hibernateProperties" value="classpath:hibernate.cfg.xml"/> 
    <!--property name="hibernateProperties"> 
     <props> 
      <prop key="current_session_context_class">thread</prop> 
      <prop key="hibernate.show_sql">true</prop> 
      <prop key="format_sql">true</prop> 
      <prop key="hibernate.use_sql_comments">true</prop> 
      <prop key="hibernate.hbm2ddl.auto">create</prop> 
      <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
      <prop key="connection.pool_size">5</prop> 
     </props> 
    </property--> 
</bean> 

<!--bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="myDataSource" /> 
    <property name="configLocation" value="classpath:hibernate.cfg.xml" /> 
    <property name="namingStrategy" ref="namingStrategy" /> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.show_sql">false</prop> 
      <prop key="hibernate.hbm2ddl.auto">create</prop> 
      <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> 
     </props> 
    </property> 
</bean--> 

<tx:annotation-driven /> 

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

<bean id="namingStrategy" class="org.hibernate.cfg.ImprovedNamingStrategy"/> 

我的連接工廠類:

public class ConnectionFactory { 

@Autowired 
private static SessionFactory sessionFactory; 

public static Session getSessionFactory() { 
    return sessionFactory.openSession(); 
} 

}

錯誤:

org.omg.CORBA.COMM_FAILURE: Detalhado: 00410001: Connection failure: socketType: IIOP_CLEAR_TEXT; hostname: localhost; port: 3700 vmcid: OMG minor code: 1 completed: No at com.sun.proxy.$Proxy40.connectFailure(Unknown Source) at com.sun.corba.ee.impl.transport.ConnectionImpl.(ConnectionImpl.java:253) at com.sun.corba.ee.impl.transport.ConnectionImpl.(ConnectionImpl.java:276) at com.sun.corba.ee.impl.transport.ContactInfoImpl.createConnection(ContactInfoImpl.java:129) at com.sun.corba.ee.impl.protocol.ClientRequestDispatcherImpl.beginRequest(ClientRequestDispatcherImpl.java:243) at com.sun.corba.ee.impl.protocol.ClientDelegateImpl.request(ClientDelegateImpl.java:227) at com.sun.corba.ee.impl.protocol.ClientDelegateImpl.is_a(ClientDelegateImpl.java:392) at org.omg.CORBA.portable.ObjectImpl._is_a(ObjectImpl.java:130) at org.omg.CosNaming.NamingContextHelper.narrow(NamingContextHelper.java:69) at com.sun.enterprise.naming.impl.SerialContext$ProviderCacheKey.getNameService(SerialContext.java:1205) at com.sun.enterprise.naming.impl.SerialContext.getRemoteProvider(SerialContext.java:393) at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:329) at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:477) at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:438) at javax.naming.InitialContext.lookup(InitialContext.java:411) at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:154) at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87) at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:152) at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:178) at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:104) at org.springframework.jndi.JndiLocatorDelegate.lookup(JndiLocatorDelegate.java:38) at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:77) at org.springframework.jndi.JndiLocatorDelegate.lookup(JndiLocatorDelegate.java:33) at org.springframework.jndi.JndiPropertySource.getProperty(JndiPropertySource.java:82) at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:73) at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:59) at org.springframework.core.env.AbstractEnvironment.getProperty(AbstractEnvironment.java:427) at org.springframework.context.support.LiveBeansView.registerApplicationContext(LiveBeansView.java:60) at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:951) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:116) at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:59) at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100) at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:248) at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:124) at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:148) at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:103) at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:73) at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:313) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) at org.junit.runner.JUnitCore.run(JUnitCore.java:157) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) Caused by: java.lang.RuntimeException: java.net.ConnectException: Connection refused: connect at org.glassfish.enterprise.iiop.impl.IIOPSSLSocketFactory.createSocket(IIOPSSLSocketFactory.java:344) at com.sun.corba.ee.impl.transport.ConnectionImpl.(ConnectionImpl.java:250) ... 61 more Caused by: java.net.ConnectException: Connection refused: connect at sun.nio.ch.Net.connect0(Native Method) at sun.nio.ch.Net.connect(Net.java:465) at sun.nio.ch.Net.connect(Net.java:457) at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:670) at com.sun.corba.ee.impl.misc.ORBUtility.openSocketChannel(ORBUtility.java:110) at org.glassfish.enterprise.iiop.impl.IIOPSSLSocketFactory.createSocket(IIOPSSLSocketFactory.java:329) ... 62 more Mai 24, 2014 5:32:32 PM org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor buildAutowiringMetadata Advertência: Autowired annotation is not supported on static fields: private static org.hibernate.SessionFactory com.math.dao.HibernateTest.sessionFactory

我試圖把Hibernate的配置只在persistence.xml,並把在Hibernate XML配置。這兩者都把我拋諸腦後。它的代碼可以看到評論bloks。

任何人都知道如何解決它?

回答

0

可能是你給你的MYSQL數據庫提供了錯誤的憑據。檢查一次... 並檢查是否從文件系統中提供了mysqlconnector.jar