User Tools

Site Tools


java:thelastpacketsuccessfullyreceivedfromserver

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
java:thelastpacketsuccessfullyreceivedfromserver [2013/10/02 09:26] – [HibernateTemplate must be a singleton] rlunarojava:thelastpacketsuccessfullyreceivedfromserver [2014/04/21 13:08] – [This crap will be useful for you if...] rlunaro
Line 13: Line 13:
   * Mysql worked perfectly   * Mysql worked perfectly
   * There wasn't a network error   * There wasn't a network error
-  * So... what the problem were???+  * So... were the problem is???
  
 ==== My architecture ==== ==== My architecture ====
Line 322: Line 322:
  
  
 +===== My final configuration of Spring =====
  
 +First, define C3P0 datasource:
  
 +<code>
 +   <!--
 +   A better datasource: it gives a new connection each time
 +   is requested.
 +   -->
 +   <bean id="dataSource"
 +         class="com.mchange.v2.c3p0.ComboPooledDataSource"
 +         destroy-method="close"
 +         scope="singleton">
 +      <!--
 +      To test a connection:
 +      mysql -h HOSTNAME -u USERNAME -p
 +      and aftwerwards introduce the password when requested
 +
 +       -->
 +      <property name="driverClass" value="com.mysql.jdbc.Driver"/>
 +      <property name="jdbcUrl"     value="jdbc:mysql://${mysql-server}:${mysql-port}/${mysql-database}"/>
 +      <property name="user"        value="${mysql-user}"/>
 +      <property name="password"    value="${mysql-password}"/>
 +      <!--
 +      Recommended values for maxPoolSize and minPoolSize:
 +          1. get value of max available connections of your
 +             database issuing command 'show variables like "max_connections";'
 +          2. this will be the upper limit for maxPoolSize: you shouldn't
 +             reach never this limit
 +          3. check out with show processlist the active connections
 +             to the database: this will help to determine the available
 +             connections for your application:
 +             (show processlist) - (show variables like max_connections)
 +          4. it is advisable to identify how many DAO objects will be
 +             issuing sentences to the database at the same time. Ok,
 +             make a rough stimation. Say that this value will be
 +             10. A good and steady value for maxPoolSize can
 +             be 40 (four times).
 +
 +       -->
 + <property name="maxPoolSize"         value="10"/>
 + <property name="minPoolSize"         value="5"/>
 + <!--
 + checkoutTimeout mits how long a client will wait for a
 + Connection, if all Connections are checked out and one
 + cannot be supplied immediately. In milliseconds.
 + http://www.mchange.com/projects/c3p0/#checkoutTimeout
 + -->
 + <property name="checkoutTimeout"     value="10000"/>
 + <property name="acquireIncrement"    value="1"/>
 + <property name="idleConnectionTestPeriod" value="100"/>
 + <!-- property name="preferredTestQuery"  value="select 1"/ -->
 + <!--
 + testConnectionOnCheckout is not recommended because it
 + checks the connection before give it to hibernate. In a
 + paranoid environment could fix some problems
 + -->
 + <!-- property name="testConnectionOnCheckout" value="true"/-->
 + <!--
 + debugUnreturnedConnectionStackTraces prints a stack trace when
 + a connection that is given to hibernate reachs its timeout value.
 + This could help to debug cases when a connection is taken and
 + not released in a certain amount of time
 + http://www.mchange.com/projects/c3p0/#debugUnreturnedConnectionStackTraces
 + -->
 + <!-- property name="debugUnreturnedConnectionStackTraces" value="true"/-->
 + <!--
 + unreturnedConnectionTimeout is the silver bullet for a problem in
 + a greedy application (in terms of sessions). It will count how long
 + hibernate has taken a connection, and if reachs a timeout, c3p0 will
 + replace this connection with a fresh session, avoiding the problem
 + of the database may close opened for much time
 + http://www.mchange.com/projects/c3p0/#unreturnedConnectionTimeout
 + -->
 + <!--property name="unreturnedConnectionTimeout" value="100"/-->
 +   </bean>
 +
 +</code>
 +
 +
 +Second, the hibernateTemplate and jdbctemplate objects of spring will be available accross the application.
 +**Beware!!! If you are developing a web application, these should be session instead of singleton!!!**
 +
 +
 +<code xml>
 +   <bean id="simpleJdbcTemplate"
 +         class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate"
 +         scope="singleton">
 +     <constructor-arg ref="dataSource"/>
 +   </bean>
 +
 +    <bean id="hibernateTemplate"
 +          class="org.springframework.orm.hibernate3.HibernateTemplate"
 +          scope="singleton">
 +        <property name="sessionFactory" ref="sessionFactory"/>
 +    </bean>
 +
 +</code>
 +
 +Finally, the sessionFactory:
 +
 +<code xml>
 +    <bean id="sessionFactory"
 +          class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
 +          scope="singleton">
 +        <property name="dataSource" ref="dataSource"/>
 +        <property name="annotatedClasses">
 +            <list>
 +                <value>com.mapfre.dga.mercurio.entity.Detail</value>
 +                <value>com.mapfre.dga.mercurio.entity.Indicator</value>
 +                <value>com.mapfre.dga.mercurio.entity.Period</value>
 +                <value>com.mapfre.dga.mercurio.entity.Struct</value>
 +                <value>com.mapfre.dga.mercurio.entity.Valoration</value>
 +                <value>com.mapfre.dga.mercurio.entity.Version</value>
 +            </list>
 +        </property>
 +        <property name="hibernateProperties">
 +            <props>
 +                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
 +                <!-- for debug, put these values to "true" -->
 +                <prop key="hibernate.show_sql">${show-sql-in-hibernate}</prop>
 +                <prop key="hibernate.format_sql">${format-sql-in-hibernate}</prop>
 +
 +                <!--
 +                Possible values for this are:
 +                     on_close : legacy behavior. The session gets the connection when
 +                                the first time it requires it and release it when it's
 +                                destroyed
 +                     after_transaction :
 +                                session is released after Transaction issues a commit or
 +                                rollback
 +                     after_statement :
 +                                connection is released after execution of every statement,
 +                                BUT it will keep the connection open if the object
 +                 -->
 +                <!-- prop key="hibernate.connection.release_mode">after_statement</prop -->
 +            </props>
 +        </property>
 +    </bean>
 +
 +</code>
 +
 +I've put plenty of comments of useful configuration parameters and lots of information, just in case. 
 +
 +===== Resources =====
 +
 +http://www.mchange.com/projects/c3p0/
  
  
 ~~DISQUS~~ ~~DISQUS~~
java/thelastpacketsuccessfullyreceivedfromserver.txt · Last modified: 2022/12/02 22:02 by 127.0.0.1