» »

tomcat in datasource

tomcat in datasource

boss-tech ::

Zanima me kako v tomcatu (linux), nastavim datasource, ki ga potem uporabim v nekem projektu?
http://tovornik.info

DavidJ ::

Kaj konkretno bi pa rad? Naredil connection pooling? Če slednje, potem uporabi tole

WebContent/META-INF/context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE connection-pooling PUBLIC 
	"-// Apache Tomcat //DTD Connection-pooling Configuration 2.0//EN"
	"http://struts.apache.org/dtds/struts-2.0.dtd">
<Context debug="5" reloadable="true" crossContext="true">
	<Resource name="jdbc/trustguard" auth="Container" type="javax.sql.DataSource"
		maxActive="100" maxIdle="30" maxWait="10000" username="user" password="pass"
		driverClassName="com.mysql.jdbc.Driver"
		url="jdbc:mysql://localhost:3306/baza?autoReconnect=true" />
</Context>


Naredi singleton za pridobivanje povezave iz poola:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class Jdbc {
	private static DataSource ds = null;

	static {
		Context initContext, envContext;

		try {
			initContext = new InitialContext();
			envContext = (Context) initContext.lookup("java:/comp/env");
			ds = (DataSource) envContext.lookup("jdbc/trustguard");
		} catch (Exception e) {
			System.err.println(e);
			System.exit(1);
		}
	}

        public static synchronized  Connection getConnection() throws NamingException, SQLException {
		return ds.getConnection();
	}
}


Kako uporabljas singleton
public List<Operator> getAllOperators() {
	// Jdbc je referenca na razred za dostop do baze
	String query = "SELECT * FROM tabELca";
	Connection connection = null;
	Statement statement = null;
	ResultSet rs = null;

	List<Operator> operators = new ArrayList<Operator>();

	try {
		connection = Jdbc.getConnection();
		statement = connection.createStatement();
		statement.executeQuery(query);
		rs = statement.getResultSet();

		int id;
		String operatorName, description;

		while (rs.next()) {
			id = rs.getInt(1);
			operatorName = rs.getString(2);
			description = rs.getString(3);

			operators.add(new Operator(id, operatorName, description));
		}
	} catch (Exception e) {
		System.err.println(e);
	} finally {
		Jdbc.closeResultSet(rs);
		Jdbc.closeStatement(statement);
		Jdbc.closeConnection(connection);
	}

	return operators;
}


edit: popravil getConnection v synchronized
"Do, or do not. There is no 'try'. "
- Yoda ('The Empire Strikes Back')

Zgodovina sprememb…

  • spremenil: DavidJ ()

DavidJ ::

Sori, pozabil na web.xml :)

WebContent/WEB-INF/web.xml
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
  <description>MySQL Test App</description>
  <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/trustguard</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>
</web-app>


Drugače pa:
- http://onjava.com/pub/a/onjava/2006/04/...
- http://developer.spikesource.com/blogs/...
- http://www.crazysquirrel.com/computing/...
- http://tomcat.apache.org/tomcat-6.0-doc...
- http://tomcat.apache.org/tomcat-6.0-doc...
- http://www.hisham.cc/articles/2008/01/0...

Pa jasno, da moraš za MySQL in ostale baze imeti ustrezen JDBC dirver.
"Do, or do not. There is no 'try'. "
- Yoda ('The Empire Strikes Back')

Zgodovina sprememb…

  • spremenil: DavidJ ()

boss-tech ::

Sem naredil na ta način. Imel sem nekaj problemov glede jdbc driverja, sedaj pa sem naletel na tole napako in me zanima če kdo ve kdaj približno bi lahko bil problem:


28.8.2009 9:28:10 org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pService' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'daoService' while setting bean property 'dao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'daoService' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'loginUserDAO' while setting bean property 'loginUserDAO'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginUserDAO' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'hibernateTemplate' while setting bean property 'hibernateTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplate' defined in ServletContext resource [/WEB-INF/applicationContext-hibernate.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext-hibernate.xml]: Invocation of init method failed; nested exception is java.lang.ExceptionInInitializerError

Related cause: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'hibernateConfig' defined in ServletContext resource [/WEB-INF/applicationContext-datasource.xml]: Unsatisfied dependency expressed through constructor argument with index 1 of type [java.lang.ClassLoader]: Ambiguous constructor argument types - did you specify the correct bean references as constructor arguments?

Related cause: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'hibernateConfig' defined in ServletContext resource [/WEB-INF/applicationContext-datasource.xml]: Unsatisfied dependency expressed through constructor argument with index 1 of type [java.lang.Class]: Ambiguous constructor argument types - did you specify the correct bean references as constructor arguments?

Related cause: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'hibernateConfig' defined in ServletContext resource [/WEB-INF/applicationContext-datasource.xml]: Unsatisfied dependency expressed through constructor argument with index 1 of type [java.lang.ClassLoader]: Ambiguous constructor argument types - did you specify the correct bean references as constructor arguments?
...
http://tovornik.info

infiniteLoop ::

Mislim, da bos moral z nami deliti vsaj tisti del spring konfiguracijske datoteke, ki se tice beana "hibernateConfig".
None of us is as dumb as all of us.

boss-tech ::

applicationContext-datasource.xml

<bean id="hibernateConfig" class="org.springframework.core.io.ClassPathResource">
	<constructor-arg index="0">
		<value type="java.lang.String">hibernate.cfg.xml</value>
	</constructor-arg>
</bean>
http://tovornik.info

boss-tech ::

src/main/resources/hibernate.cfg.xml pa ima tole vsebino:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
		"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</property>
        <!-- property name="hibernate.hbm2ddl.auto">create-drop</property -->
        <property name="hibernate.show_sql">false</property>
        <property name="hibernate.default_schema">test</property>
        <property name="hibernate.jdbc.batch_size">30</property>
    </session-factory>
</hibernate-configuration>
http://tovornik.info

Zgodovina sprememb…

infiniteLoop ::

Katero verzijo springa pa uporabljas? Jaz sem probal na 2.5.6 in dela OK. Zgleda, da je problem z argument za hibernateConfig bean sodec po stack traceu.

Zakaj pa sploh poskusas ustvariti poseben ClassPath resource? To lahko zate opravi spring implicitno. Primer:

<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
    <property name="dataSource"><ref local="dataSource"/></property>
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
</bean>
None of us is as dumb as all of us.

boss-tech ::

Imam 2.5.4. Stvar je v tem da mora sessionFactory dobit ta configLocation v obliki objekta(ker bean hibernetConfig je objekt razreda org.springframework.core.io.ClassPathResource), ne vem ali je ta classpath:hibernate.cfg.xml objekt ali string?

<bean id="hibernateConfig" class="org.springframework.core.io.ClassPathResource">
		<constructor-arg>			
			<value>hibernate-itrackpersonal.cfg.xml</value>			
		</constructor-arg>
</bean>
http://tovornik.info

infiniteLoop ::

Spring bo string "classpath:hibernate.cfg.xml" implicitno spremenil v resource.
None of us is as dumb as all of us.

boss-tech ::

Sem naredil takole:

<bean id="sessionFactory" class="...core.spring.annotation.AnnotationSessionFactoryBean"> //lokalni razred ki extenda     org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
	     
	    <property name="dataSource"><ref bean="dataSource"/></property>
	    
  		<property name="configLocation">
  			<value>classpath:hibernate.cfg.xml</value>			
		</property>

...


Pa dobim napako:


1.9.2009 14:14:31 org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'Service' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'daoService' while setting bean property 'dao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'daoService' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'loginUserDAO' while setting bean property 'loginUserDAO'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginUserDAO' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'hibernateTemplate' while setting bean property 'hibernateTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplate' defined in ServletContext resource [/WEB-INF/applicationContext-hibernate.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext-hibernate.xml]: Invocation of init method failed; nested exception is java.lang.ExceptionInInitializerError
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:275)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1244)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1008)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:470)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:220)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:729)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:381)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3843)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4338)
at org.apache.catalina.manager.ManagerServlet.start(ManagerServlet.java:1247)
at org.apache.catalina.manager.HTMLManagerServlet.start(HTMLManagerServlet.java:604)
at org.apache.catalina.manager.HTMLManagerServlet.doGet(HTMLManagerServlet.java:129)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at sun.reflect.GeneratedMethodAccessor57.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:244)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAsPrivileged(Subject.java:537)
at org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:276)
at org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:162)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:283)
at org.apache.catalina.core.ApplicationFilterChain.access$000(ApplicationFilterChain.java:56)
at org.apache.catalina.core.ApplicationFilterChain$1.run(ApplicationFilterChain.java:189)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:185)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:525)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:636)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'daoService' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'loginUserDAO' while setting bean property 'loginUserDAO'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginUserDAO' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'hibernateTemplate' while setting bean property 'hibernateTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplate' defined in ServletContext resource [/WEB-INF/applicationContext-hibernate.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext-hibernate.xml]: Invocation of init method failed; nested exception is java.lang.ExceptionInInitializerError
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:275)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1244)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1008)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:470)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:220)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:269)
... 49 more

...

http://tovornik.info

Zgodovina sprememb…

infiniteLoop ::

Ko postas stacktrace je najbolj zanimiv ozr. uporaben root cause. Torej zadnji del stack tracea ozr. zadnji "Caused by" in do konca. Ce hoces pomoc nam ga zrihtaj ;)
None of us is as dumb as all of us.

boss-tech ::

Tule je celoten stacktrace
http://tovornik.info

infiniteLoop ::

Eh majster to ti je pa spet novi stack trace, ki kaze na to, da imas spet probleme z "hibernateConfig". Flikni to ven in poskusi z implicitnim resourcom. Potem pa postaj stack trace od napake, ki si jo dobil tam.
None of us is as dumb as all of us.


Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

Ne zazna R.class -android

Oddelek: Programiranje
17834 (672) Marat
»

[Android]Vprasanje glede GUI ter pripadajoce XML datoteke

Oddelek: Programiranje
102041 (1927) KernelPanic
»

java datapool - derby

Oddelek: Programiranje
61351 (1200) jlpktnst
»

java, zajem podatkov iz DB

Oddelek: Programiranje
10975 (707) l0g1t3ch
»

Java- hibernate.properties not found

Oddelek: Programiranje
111255 (755) xsenon

Več podobnih tem