SQLNestedException

4 respostas
A

Pessoal,

estou tentando fazer a persistencia de dados através do tutorial do Mauricio Linhares. Correu tudo bem até ao momento em que fui testar com o Junit o seguinte teste:

public class SpringTestExemplos extends TestCase {

	private ApplicationContext context;


	protected void setUp() throws Exception {
		super.setUp();
		context = new ClassPathXmlApplicationContext("applicationContext.xml");
	}

	public void testInserirCurso() {
		GenericDao dao = (GenericDao) context.getBean("daoGenericoTransacional");

		Integer quantidadeAnterior = dao.list(Curso.class).size();

		Curso curso = new Curso();

		curso.setNome("DSI");
		curso.setDescricao("Desenvolvimento de Software Para internet");

		dao.save(curso);

		if ( quantidadeAnterior.equals( dao.list(Curso.class) ) ) {
			this.fail("A quantidade de cursos deveria ter sido aumentada");
		}
	}
}

O applicationContext:

<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">


<beans>


  <bean id="daoAlvo"
      class="br.edu.cefetpb.spring.HibernateGenericDao">
      <property name="sessionFactory">
        <ref local="sessionFactory"/>
      </property>
  </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
          <ref local="dataSource"/>
        </property>
        <property name="mappingResources">
          <value>
			br/edu/cefetpb/Curso.hbm.xml,
			br/edu/cefetpb/Disciplina.hbm.xml,
			br/edu/cefetpb/Turma.hbm.xml,
			br/edu/cefetpb/Pessoa.hbm.xml,
			br/edu/cefetpb/Aluno.hbm.xml,
			br/edu/cefetpb/Professor.hbm.xml,
			br/edu/cefetpb/Endereco.hbm.xml
          </value>
        </property>
        <property name="hibernateProperties">
          <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="show_sql">true</prop>
            <prop key="hibernate.generate_statistics">true</prop>
            <prop key="hibernate.use_sql_comments">true</prop>
            <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
          </props>
        </property>
    </bean>

    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
      <property name="url">
        <value>	jdbc:mysql://localhost:3306/linharesdb</value>
      </property>
      <property name="username">
        <value>root</value>
      </property>
      <property name="password">
        <value>pass</value>
      </property>
      <property name="driverClassName">
        <value>com.mysql.jdbc.Driver</value>
      </property>
      <property name="initialSize">
        <value>5</value>
      </property>
      <property name="maxActive">
        <value>20</value>
      </property>
      <property name="maxIdle">
        <value>5</value>
      </property>
      <property name="poolPreparedStatements">
        <value>true</value>
      </property>
    </bean>

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

    <bean id="daoGenericoTransacional"
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
      <property name="transactionManager">
        <ref local="transactionManager"/>
      </property>
      <property name="target">
        <ref local="daoAlvo"/>
      </property>
      <property name="transactionAttributes">
        <props>
          <prop key="delete*">
            PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED
          </prop>
          <prop key="save*">
            PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED
          </prop>
          <prop key="update*">
            PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED
          </prop>
          <prop key="get*">
            PROPAGATION_SUPPORTS, readOnly
          </prop>
          <prop key="list*">
            PROPAGATION_SUPPORTS, readOnly
          </prop>
        </props>
      </property>
    </bean>

	<bean id="gerenciadorDeTransferenciasAlvo"
			class="br.edu.cefetpb.banco.GerenciadorDeTransferencias">
		<property name="genericDao">
			 <ref bean="daoAlvo"/>
		</property>
	</bean>

    <bean id="gerenciadorDeTransferencias"
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
      <property name="transactionManager">
        <ref local="transactionManager"/>
      </property>
      <property name="target">
        <ref local="gerenciadorDeTransferenciasAlvo"/>
      </property>
      <property name="transactionAttributes">
        <props>
          <prop key="transferir*">
            PROPAGATION_REQUIRED, ISOLATION_SERIALIZABLE
          </prop>
        </props>
      </property>
    </bean>

</beans>

Ocorre o seguinte erro:

01:14:27,880  INFO ConnectionProviderFactory:72 - Initializing connection provider: org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider
01:14:27,890 DEBUG JDBCExceptionReporter:63 - SQL Exception
org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class 'com.mysql.jdbc.Driver' for connect URL '	jdbc:mysql://localhost:3306/linharesdb'
	at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:780)
	at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:540)
	at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:81)
	at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:73)
	at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:1928)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1211)
	at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:807)
	at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:740)
	at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:131)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1062)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1029)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:420)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:245)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:141)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:242)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:156)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:246)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:128)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:955)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:729)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:416)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:245)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:141)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:242)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:156)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:290)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:348)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:92)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:77)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:68)
	at br.edu.cefetpb.hibernate.exemplo.SpringTestExemplos.setUp(SpringTestExemplos.java:31)
	at junit.framework.TestCase.runBare(TestCase.java:125)
	at junit.framework.TestResult$1.protect(TestResult.java:106)
	at junit.framework.TestResult.runProtected(TestResult.java:124)
	at junit.framework.TestResult.run(TestResult.java:109)
	at junit.framework.TestCase.run(TestCase.java:118)
	at junit.framework.TestSuite.runTest(TestSuite.java:208)
	at junit.framework.TestSuite.run(TestSuite.java:203)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: java.sql.SQLException: No suitable driver
	at java.sql.DriverManager.getDriver(Unknown Source)
	at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:773)
	... 40 more

Já coloquei o driver "mysql-connector-java" em C:\Programas\Tomcat 5.5\bin .... está na classpath inclusive pesquisei/testei nos seguintes endereços (entre outros) e nada!:

http://tomcat.apache.org/tomcat-5.5-doc/jndi-datasource-examples-howto.html
http://www.laliluna.de/260.html
http://www.evolutionnext.com/blog/2005/10/13/1129259088959.html

Afinal, como se faz isso direitinho? Em alguns a configuraçao é no server.xml noutros é no context.xml :???:

Isto ta difícil... :(

Agradeço toda a ajuda!

4 Respostas

G

Você colocou o JAR do MySQL na pasta WEB-INF/lib ?

A

Sim.. está lá tb.

O problema é mesmo a configuração do context , pk eu com outros testes consigo inserir dados no banco.

Como configurar o context? Esta configuração do web.xml do site do apache.. coloco no applicationContext.xml.. junto com os beans?

3. web.xml configuration

Now create a WEB-INF/web.xml for this test application.
		
	
  <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/TestDB</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>
</web-app>
M

mas a lib do tomcat não é no /tomcat_home/bin e sim /tomcat_home/commons/lib.

:okok:

A

Obrigado! O problema está resolvido. Acho que era porque tinha duas versões diferentes do mmo .jar no lib.

Cumprimentos

Criado 17 de outubro de 2006
Ultima resposta 19 de out. de 2006
Respostas 4
Participantes 3