IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Spring Java Discussion :

problème avec dataSource


Sujet :

Spring Java

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Décembre 2005
    Messages
    37
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2005
    Messages : 37
    Points : 23
    Points
    23
    Par défaut problème avec dataSource
    Bonjour,
    la meilleure facon de vous exposer mon probléme c'est de vous montrer mon code :

    mon fichier applicationContext.xml
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
     
    <?xml version="1.0" encoding="ISO_8859-1"?>
    <!DOCTYPE beans SYSTEM "http://www.springframework.org/dtd/spring-beans.dtd">
     
    <beans>
     
    	<!-- bean datasource -->
    	<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      			<property name="driverClassName">
        			<value>com.sybase.jdbc2.jdbc.SybDriver</value>
      			</property>
      			<property name="url">
        			<value>jdbc:sybase:Tds:xxx.xxx.xxx.xxx:xxxx/dbParameter</value>
      			</property>
      			<property name="username">
        			<value>xxxx</value>
      			</property>
      			<property name="password">
        			<value>xxxx</value>
      			</property>
    	</bean>
     
    	<!-- L'implémentation de la couche d'access aux données de type Groupe -->
    	<bean id="paramGroupDaoImpl" class="monPackage.ParamGroupDAOImpl" >
    		<property name="ds">
    			<ref local="ds"/>
    		</property>
    	</bean>
     
        <!--  L'implémentation de la couche métier de type Groupe -->
    	<bean id="paramGroupManagerImpl" class="monPackage.ParamGroupManagerImpl">
    		<property name="dao">
    			<ref local="paramGroupDaoImpl" />
    		</property>
    	</bean>
     
    </beans>
    voici ma classe ParamGroupDAOImpl qui implémente l'interface ParamDAO :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
     
    public class ParamGroupDAOImpl implements ParamDAO {
     
     
     
    	private DriverManagerDataSource ds;
     
    	JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
     
     
    	// Getter
    	public DriverManagerDataSource getDs() {
    			return ds;
    	}
     
    	// Setter
    	public void setDs(DriverManagerDataSource ds) {
    			this.ds = ds;
    	}
     
    	public ArrayList<ParamGroupBean> selectAll() {
     
    		ArrayList<ParamGroupBean> listGroupBean = new ArrayList<ParamGroupBean>();
    		String sql = "SELECT * FROM PARAM_GROUPS";
    		try{
    		RowMapper mapper = new RowMapper() {
    	        public ParamGroupBean mapRow(ResultSet rs, int rowNum) throws SQLException {
     
    	        	ParamGroupBean groupBean = new ParamGroupBean();
    	        	groupBean.setId(rs.getLong(1));
    	        	groupBean.setName(rs.getString(2));
    	        	groupBean.setGroupParentId(rs.getLong(3));
    	        	groupBean.setStatus(rs.getInt(4));
    	        	return groupBean;
    	        }
    	    };
     
    	    listGroupBean.add((ParamGroupBean) jdbcTemplate.queryForObject(sql, mapper));
    		}catch(Exception e){
    			new LunamanagerException( "Une exception s'est produite dans la classe "+this.getClass().getName()+" : + "+e.getMessage(),1);
    		}
    		return listGroupBean;
    	}
     
    }
    et voici ma classe service ParamGroupManagerImpl :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
     
    public class ParamGroupManagerImpl implements ParamGroupManager{
     
    	private ParamDAO dao; // mon interface
     
     
    	// constructeur par defaut
    	public ParamGroupManagerImpl() {
    	}
     
     
    	// setter
    	public void setDao(ParamDAO dao) {
    		this.dao = dao;
    	}
     
    	// getter
    	public ParamDAO getDao() {
    		return dao;
    	}
     
    	// synchronized pour empecher les acces multiples aux données, vu que la classe service est un singleton
    	// retourne une liste contenant tous les groupes
    	public synchronized ArrayList<ParamGroupBean> getAll() {
     
    		ArrayList<ParamGroupBean> _groups = new ArrayList<ParamGroupBean>();
    		_groups = (ArrayList<ParamGroupBean>) dao.selectAll();
     
    		return _groups;
    	} 
     
    }
     
    // mon interface ParamDAO 
    public interface ParamDAO {
     
      public Object selectAll();
     
    }
    et voici l'erreur comme quoi il trouve pas le dataSource :

    [13:17:35.280] Server[] starting
    [13:17:35.280]
    [13:17:35.280] Windows XP 5.1 x86
    [13:17:35.280] Java 1.5.0_08-b03, 32, mixed mode, sharing, Cp1252, fr, Sun Microsystems Inc.
    [13:17:35.280] resin.home = C:\resin-pro-3.0.18
    [13:17:35.280] server.root = C:\resin-pro-3.0.18
    [13:17:35.280]
    [13:17:35.374] Socket JNI library requires a valid Resin Professional License.
    [13:17:35.374] See http://www.caucho.com/sales for information.
    [13:17:35.374] http listening to *:8080
    [13:17:35.437] hmux listening to localhost:6802
    [13:17:35.530] Host[] starting
    [13:17:35.812] In-place class redefinition (HotSwap) is not available. In-place class reloading during development requires a compatible JDK and -Xdebug.
    [13:17:35.984] WebApp[http://localhost:8080] starting
    log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
    log4j:WARN Please initialize the log4j system properly.
    [13:17:36.124] Loading Spring root WebApplicationContext
    [13:17:37.077] org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'paramGroupDaoImpl' defined in ServletContext resource [/WEB-INF/applicationC
    ntext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [fr.framework.manager.paramete
    .sybase.dao.ParamGroupDAOImpl]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: dataSource is required
    [13:17:37.077] Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [fr.framework.manager.parameter.sybase.dao.ParamGroupDAOImpl]: Con
    tructor threw exception; nested exception is java.lang.IllegalArgumentException: dataSource is required
    [13:17:37.077] Caused by: java.lang.IllegalArgumentException: dataSource is required
    [13:17:37.077] at org.springframework.jdbc.support.JdbcAccessor.afterPropertiesSet(JdbcAccessor.java:130)
    [13:17:37.077] at org.springframework.jdbc.core.JdbcTemplate.<init>(JdbcTemplate.java:141)
    [13:17:37.077] at fr.framework.manager.parameter.sybase.dao.ParamGroupDAOImpl.<init>(ParamGroupDAOImpl.java:24)
    [13:17:37.077] at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    [13:17:37.077] at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    [13:17:37.077] at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    [13:17:37.077] at java.lang.reflect.Constructor.newInstance(Constructor.java:494)
    [13:17:37.077] at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:84)
    [13:17:37.077] at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:60)
    [13:17:37.077] at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:52)
    [13:17:37.077] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:640)
    [13:17:37.077] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:626)
    [13:17:37.077] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:381)
    [13:17:37.077] at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:245)
    [13:17:37.077] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:140)
    [13:17:37.077] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:242)
    [13:17:37.077] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:156)
    [13:17:37.077] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:273)
    [13:17:37.077] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:346)
    [13:17:37.077] at org.springframework.web.context.support.AbstractRefreshableWebApplicationContext.refresh(AbstractRefreshableWebApplicationContext.java:156)
    [13:17:37.077] at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:246)
    [13:17:37.077] at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:184)
    [13:17:37.077] at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:49)
    [13:17:37.077] at com.caucho.server.webapp.Application.start(Application.java:1592)
    [13:17:37.077] at com.caucho.server.deploy.DeployController.startImpl(DeployController.java:587)
    [13:17:37.077] at com.caucho.server.deploy.StartAutoRedeployAutoStrategy.startOnInit(StartAutoRedeployAutoStrategy.java:72)
    [13:17:37.077] at com.caucho.server.deploy.DeployController.startOnInit(DeployController.java:475)
    [13:17:37.077] at com.caucho.server.deploy.DeployContainer.start(DeployContainer.java:158)
    [13:17:37.077] at com.caucho.server.webapp.ApplicationContainer.start(ApplicationContainer.java:651)
    [13:17:37.077] at com.caucho.server.host.Host.start(Host.java:385)
    [13:17:37.077] at com.caucho.server.deploy.DeployController.startImpl(DeployController.java:587)
    [13:17:37.077] at com.caucho.server.deploy.StartAutoRedeployAutoStrategy.startOnInit(StartAutoRedeployAutoStrategy.java:72)
    [13:17:37.077] at com.caucho.server.deploy.DeployController.startOnInit(DeployController.java:475)
    [13:17:37.077] at com.caucho.server.deploy.DeployContainer.start(DeployContainer.java:158)
    [13:17:37.077] at com.caucho.server.host.HostContainer.start(HostContainer.java:467)
    [13:17:37.077] at com.caucho.server.resin.ServletServer.start(ServletServer.java:945)
    [13:17:37.077] at com.caucho.server.deploy.DeployController.startImpl(DeployController.java:587)
    [13:17:37.077] at com.caucho.server.deploy.AbstractDeployControllerStrategy.start(AbstractDeployControllerStrategy.java:56)
    [13:17:37.077] at com.caucho.server.deploy.DeployController.start(DeployController.java:483)
    [13:17:37.077] at com.caucho.server.resin.ResinServer.start(ResinServer.java:478)
    [13:17:37.077] at com.caucho.server.resin.Resin.init(Resin.java)
    [13:17:37.077] at com.caucho.server.resin.Resin.main(Resin.java:623)

    Merci de m'aider.

  2. #2
    Membre régulier
    Inscrit en
    Octobre 2002
    Messages
    108
    Détails du profil
    Informations forums :
    Inscription : Octobre 2002
    Messages : 108
    Points : 98
    Points
    98
    Par défaut
    Le problème se trouve à la ligne en rouge. La datasource n'a pas été initialisée à ce moment là.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    public class ParamGroupDAOImpl implements ParamDAO {
     
    	
    	
    	private DriverManagerDataSource ds;
    	             
    	JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
     
    	
    	// Getter
    	public DriverManagerDataSource getDs() {
    			return ds;
    	}
     
    	// Setter
    	public void setDs(DriverManagerDataSource ds) {
    			this.ds = ds;
    	}
    		
    	public ArrayList<ParamGroupBean> selectAll() {
    		
    		ArrayList<ParamGroupBean> listGroupBean = new ArrayList<ParamGroupBean>();
    		String sql = "SELECT * FROM PARAM_GROUPS";
    		try{
    		RowMapper mapper = new RowMapper() {
    	        public ParamGroupBean mapRow(ResultSet rs, int rowNum) throws SQLException {
    	        	
    	        	ParamGroupBean groupBean = new ParamGroupBean();
    	        	groupBean.setId(rs.getLong(1));
    	        	groupBean.setName(rs.getString(2));
    	        	groupBean.setGroupParentId(rs.getLong(3));
    	        	groupBean.setStatus(rs.getInt(4));
    	        	return groupBean;
    	        }
    	    };
     
    	    listGroupBean.add((ParamGroupBean) jdbcTemplate.queryForObject(sql, mapper));
    		}catch(Exception e){
    			new LunamanagerException( "Une exception s'est produite dans la classe "+this.getClass().getName()+" : + "+e.getMessage(),1);
    		}
    		return listGroupBean;
    	}
    	
    }

  3. #3
    Membre à l'essai
    Profil pro
    Inscrit en
    Décembre 2005
    Messages
    37
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2005
    Messages : 37
    Points : 23
    Points
    23
    Par défaut
    Merci trungsi, c'était bien ça le probléme.
    j'ai changé mon fichier application context en injectant la dataSource dans le jdbcTemplate comme ce ci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
     
    <?xml version="1.0" encoding="ISO_8859-1"?>
    <!DOCTYPE beans SYSTEM "http://www.springframework.org/dtd/spring-beans.dtd">
     
    <beans>
     
    	<!-- bean datasource -->
    	<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      			<property name="driverClassName">
        			<value>com.sybase.jdbc2.jdbc.SybDriver</value>
      			</property>
      			<property name="url">
        			<value>jdbc:sybase:Tds:xxx.xxx.xxx.xxx:xxxx/dbParameter</value>
      			</property>
      			<property name="username">
        			<value>xxxx</value>
      			</property>
      			<property name="password">
        			<value>xxxx</value>
      			</property>
    	</bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
    		<property name="dataSource">
    			<ref local="dataSource"/>
    		</property>
    		</bean>
     
    	<!-- L'implémentation de la couche d'access aux données de type Groupe -->
    	<bean id="paramGroupDaoImpl" class="fr.framework.manager.parameter.sybase.dao.ParamGroupDAOImpl">
    		<property name="jdbcTemplate">
    			<ref local="jdbcTemplate"/>
    		</property>
    	</bean>
     
        <!--  L'implémentation de la couche métier de type Groupe -->
    	<bean id="paramGroupManagerImpl" class="fr.framework.manager.parameter.service.ParamGroupManagerImpl">
    		<property name="dao">
    			<ref local="paramGroupDaoImpl" />
    		</property>
    	</bean>
     
    </beans>
    et donc là si j'ai bien compris, j'instancie ma connexion au chargement du fichier applicationContext.xml, du coup ma classe qui implémente ma DAO est la suivante :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
     
    public class ParamGroupDAOImpl implements ParamDAO {
     
    	private JdbcTemplate jdbcTemplate;
     
    	public JdbcTemplate getJdbcTemplate() {
    		return jdbcTemplate;
    	}
     
     
    	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    		this.jdbcTemplate = jdbcTemplate;
    	}
     
     
    	public ArrayList<ParamGroupBean> selectAll() {
     
    		ArrayList<ParamGroupBean> listGroupBean = new ArrayList<ParamGroupBean>();
    		String sql = "SELECT * FROM PARAM_GROUP";
    		try{
    		RowMapper mapper = new RowMapper() {
    	        public ParamGroupBean mapRow(ResultSet rs, int rowNum) throws SQLException {
     
    	        	ParamGroupBean groupBean = new ParamGroupBean();
    	        	groupBean.setId(rs.getLong(1));
    	        	groupBean.setName(rs.getString(2));
    	        	groupBean.setGroupParentId(rs.getLong(3));
    	        	groupBean.setStatus(rs.getInt(4));
    	        	return groupBean;
    	        }
    	    };
     
    	    listGroupBean.add((ParamGroupBean) jdbcTemplate.queryForObject(sql, mapper));
    		}catch(Exception e){
    			new LunamanagerException( "Une exception s'est produite dans la classe "+this.getClass().getName()+" : + "+e.getMessage(),1);
    		}	
     
    		return listGroupBean;
    	}
    }
    j'ai testé et ça marche.
    J'ai deux probléme avec cette approche :
    le premier , c'est que je souhaite pas ouvrir une connexion avec le chargement de "applicationContext.xml", je souhaiterais ouvrir la connexion quand je veux et le fermer quand je veux.
    le deuxiéme, c'est que dans la classe JdbcTemplate, je trouve pas de méthode pour fermer la connexion, un truc genre "jdbcTemplate.close()" par exmple ...
    est ce normal ? est ce Spring qui ferme la connexion aprés tel ou tel requéte et comment il gére ça... je séche royalement...
    pouvez vous m'aider ?
    merci.

  4. #4
    Inactif  
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    2 189
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : Suisse

    Informations forums :
    Inscription : Mai 2006
    Messages : 2 189
    Points : 2 336
    Points
    2 336
    Par défaut
    Tu utilises quoi comme ORM, je comprend pas si tu utilises Hibernate que tu doives gérer ca toi même ... Utilises HibernateTemplate et ca uniquement dans ton applicationContext pas besoin de classe à définir pour ca, c'est gérer par Spring ...

  5. #5
    Expert confirmé
    Profil pro
    Inscrit en
    Août 2006
    Messages
    3 274
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 3 274
    Points : 4 141
    Points
    4 141
    Par défaut
    Alexandre, il utilise Jdbc pas Hibernate !

  6. #6
    Membre confirmé
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    365
    Détails du profil
    Informations personnelles :
    Localisation : Maroc

    Informations forums :
    Inscription : Janvier 2006
    Messages : 365
    Points : 495
    Points
    495
    Par défaut
    Citation Envoyé par linniesurf
    j'ai testé et ça marche.
    J'ai deux probléme avec cette approche :
    le premier , c'est que je souhaite pas ouvrir une connexion avec le chargement de "applicationContext.xml", je souhaiterais ouvrir la connexion quand je veux et le fermer quand je veux.
    le deuxiéme, c'est que dans la classe JdbcTemplate, je trouve pas de méthode pour fermer la connexion, un truc genre "jdbcTemplate.close()" par exmple ...
    est ce normal ? est ce Spring qui ferme la connexion aprés tel ou tel requéte et comment il gére ça... je séche royalement...
    pouvez vous m'aider ?
    merci.
    Salut,
    Si tu utilises le framework Spring c'est justement pour que celui-ci puisse s'occuper, entre autres, de la gestion des ressources, des transactions, des exceptions ... et te permettre de te concentrer sur ce qui est spécifique à ton application, tes requêtes sql, par exemple. Et là en l'occurence, la datasource est une ressource dont tu délègues la gestion à Spring (ouverture, fermeture de connexions, gestion des exceptions bd, commit et rollback des transactions...) à travers le JdbcTemplate. Si tu devrais ouvrir et fermer les connexions toi-même, il faudrait aussi que ton code devienne pollué par des blocks try/catch/finally qui accompagnent les connection.close(), statement.close() et autres, et tu perdrais l'avantage d'utiliser Spring.
    Voilà un petit peu ce que je pourrais dire là-dessus, si cela peut t'aider.
    Concernant la classe org.springframework.jdbc.datasource.DriverManagerDataSource, il me semble qu'elle sert surtout pour les tests, en dehors de tout conteneur web, et qu'en production tu devrais plutot configurer un nom jndi et utiliser celle fournie par ton conteneur, ou bien par exemple Commons DBCP. enfin, c'est ce que j'ai lu dans la doc Spring ...
    SCJP 5 / SCBCD 1.3 Certified

Discussions similaires

  1. problème avec Datasource d'un MsFlexgrid
    Par yrifka dans le forum ASP.NET
    Réponses: 0
    Dernier message: 19/07/2010, 11h40
  2. Problème avec DataSource
    Par sebarbraz dans le forum Struts 1
    Réponses: 9
    Dernier message: 20/11/2007, 23h40
  3. problème de connexion avec datasource xml
    Par Flash31186 dans le forum iReport
    Réponses: 4
    Dernier message: 25/10/2007, 09h38
  4. [vb.net] problème avec indices d'un DataSource
    Par A2rem dans le forum Windows Forms
    Réponses: 1
    Dernier message: 14/09/2006, 16h14
  5. probléme avec un datasource sur tomcat et hibernate
    Par senediene dans le forum Hibernate
    Réponses: 4
    Dernier message: 21/08/2006, 18h50

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo