在hibernate的配置里面不是有个lazy么?设为true时,会在实际需要SQL结果集才执行SQL,减少内存的开销呀!

解决方案 »

  1.   

    SessionFactory是线程安全的,没有必要每次都要重新生成一个SessionFactory
    你说每次都要进行一次映射,很明显是每次都使用一个新的SessionFactory造成的SessionFactory是重量级的组件,一般一个应用程序对应一个数据库只用一个SessionFactory就够了,你需要生成的是session,那玩意生成的代价就小了,session是轻量级的但是不是线程安全,所以需要每个数据库操作都必须生成一个新的session
      

  2.   

    我的applicationcontext.xml文件如下
    <beans>
    <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    destroy-method="close">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
    <property name="url" value="jdbc:oracle:thin:@localhost:1521:ksxt"/>
    <property name="username" value="bb"/>
    <property name="password" value="aa"/>
    </bean>
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
    <ref local="dataSource"/>
    </property>
    <property name="mappingResources">
    <list>
    <value>model/Password.hbm.xml</value>
    <value>model/Visitcount.hbm.xml</value>
    <value>model/Infoissue.hbm.xml</value>
    <value>model/Student.hbm.xml</value>
    <value>model/Studentselectcourse.hbm.xml</value>
    <value>model/Course.hbm.xml</value>
    <value>model/Teacher.hbm.xml</value>
    <value>model/Teachcourse.hbm.xml</value>
    <value>model/Teststate.hbm.xml</value>
    <value>model/Score.hbm.xml</value>
    <value>model/Major.hbm.xml</value>
    <value>model/Dept.hbm.xml</value>
    <value>model/Userwords.hbm.xml</value>
    <value>model/Testpaper.hbm.xml</value>
    <value>model/Singlepaper.hbm.xml</value>
    <value>model/Singletheme.hbm.xml</value>
    <value>model/Singletype.hbm.xml</value>
    <value>model/Savesingle.hbm.xml</value>
    <value>model/Globevar.hbm.xml</value>
    <value>model/Downloadissue.hbm.xml</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.max_fetch_depth">10</prop>
    <prop key="hibernate.cache.use_query_cache">true</prop>
    <prop key="hibernate.jdbc.batch_size">0</prop>
    <prop key="hibernate.c3p0.min_size">5</prop>
    <prop key="hibernate.c3p0.max_size">20</prop>
    <prop key="hibernate.c3p0.timeout">1800</prop>
    <prop key="hibernate.c3p0.max_statements">50</prop>
    <prop key="hibernate.c3p0.acquire_increment">2</prop>
    <prop key="hibernate.c3p0.idle_test_period">3000</prop>
    <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
    </props>
    </property>
    </bean>
    <bean id="PasswordDAO" class="model.PasswordDAO">
    <property name="sessionFactory">
    <ref bean="sessionFactory"/>
    </property>
    </bean>
    <bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
    <ref local="sessionFactory"/>
    </property>
    </bean>
    <bean id="PasswordDAOProxy"
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager">
    <ref bean="transactionManager"/>
    </property>
    <property name="target">
    <ref local="PasswordDAO"/>
    </property>
    <property name="transactionAttributes">
    <props>
    <prop key="insert*">PROPAGATION_REQUIRED</prop>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="delete*">PROPAGATION_REQUIRED</prop>
    <prop key="*">PROPAGATION_REQUIRED, readOnly</prop>
    </props>
    </property>
    </bean>
    <bean id="VisitcountDAO" class="model.VisitcountDAO">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>
    <bean id="VisitcountDAOProxy"
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager">
    <ref bean="transactionManager"/>
    </property>
    <property name="target">
    <ref local="VisitcountDAO"/>
    </property>
    <property name="transactionAttributes">
    <props>
    <prop key="insert*">PROPAGATION_REQUIRED</prop>
    <prop key="*">PROPAGATION_REQUIRED, readOnly</prop>
    </props>
    </property>
    </bean>
    <bean id="InfoissueDAO" class="model.InfoissueDAO">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>
    <bean id="InfoissueDAOProxy"
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager">
    <ref bean="transactionManager"/>
    </property>
    <property name="target">
    <ref local="InfoissueDAO"/>
    </property>
    <property name="transactionAttributes">
    <props>
    <prop key="insert*">PROPAGATION_REQUIRED</prop>
    <prop key="*">PROPAGATION_REQUIRED, readOnly</prop>
    </props>
    </property>
    </bean>
    ......
    </beans>
      

  3.   

    打开首页时,显示的信息如下
    2006-07-15 08:54:30,125 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - <Loading XML bean definitions from class path resource [applicationContext.xml]>
    2006-07-15 08:54:31,671 INFO [org.springframework.aop.framework.DefaultAopProxyFactory] - <CGLIB2 available: proxyTargetClass feature enabled>
    2006-07-15 08:54:31,937 INFO [org.springframework.jdbc.datasource.DriverManagerDataSource] - <Loaded JDBC driver: oracle.jdbc.driver.OracleDriver>
    2006-07-15 08:54:31,984 INFO [org.hibernate.cfg.Environment] - <Hibernate 3.0.5>
    2006-07-15 08:54:31,984 INFO [org.hibernate.cfg.Environment] - <hibernate.properties not found>
    2006-07-15 08:54:32,000 INFO [org.hibernate.cfg.Environment] - <using CGLIB reflection optimizer>
    2006-07-15 08:54:32,000 INFO [org.hibernate.cfg.Environment] - <using JDK 1.4 java.sql.Timestamp handling>
    2006-07-15 08:54:32,812 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Password -> PASSWORD>
    2006-07-15 08:54:32,906 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Visitcount -> VISITCOUNT>
    2006-07-15 08:54:32,937 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Infoissue -> INFOISSUE>
    2006-07-15 08:54:32,968 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Student -> STUDENT>
    2006-07-15 08:54:33,000 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Studentselectcourse -> STUDENTSELECTCOURSE>
    2006-07-15 08:54:33,093 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Course -> COURSE>
    2006-07-15 08:54:33,125 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Teacher -> TEACHER>
    2006-07-15 08:54:33,171 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Teachcourse -> TEACHCOURSE>
    2006-07-15 08:54:33,453 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Teststate -> TESTSTATE>
    2006-07-15 08:54:33,484 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Score -> SCORE>
    2006-07-15 08:54:33,515 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Major -> MAJOR>
    2006-07-15 08:54:33,562 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Dept -> DEPT>
    2006-07-15 08:54:33,593 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Userwords -> USERWORDS>
    2006-07-15 08:54:33,609 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Testpaper -> TESTPAPER>
    2006-07-15 08:54:33,640 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Singlepaper -> SINGLEPAPER>
    2006-07-15 08:54:33,703 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Singletheme -> SINGLETHEME>
    2006-07-15 08:54:33,734 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Singletype -> SINGLETYPE>
    2006-07-15 08:54:33,781 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Savesingle -> SAVESINGLE>
    2006-07-15 08:54:33,875 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Globevar -> GLOBEVAR>
    2006-07-15 08:54:33,906 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Downloadissue -> DOWNLOADISSUE>
    2006-07-15 08:54:33,906 INFO [org.springframework.orm.hibernate3.LocalSessionFactoryBean] - <Building new Hibernate SessionFactory>
    2006-07-15 08:54:33,906 INFO [org.hibernate.cfg.Configuration] - <processing extends queue>
    2006-07-15 08:54:33,906 INFO [org.hibernate.cfg.Configuration] - <processing collection mappings>
    2006-07-15 08:54:33,906 INFO [org.hibernate.cfg.HbmBinder] - <Mapping collection: model.Major.students -> STUDENT>
    2006-07-15 08:54:33,906 INFO [org.hibernate.cfg.HbmBinder] - <Mapping collection: model.Dept.majors -> MAJOR>
    2006-07-15 08:54:33,906 INFO [org.hibernate.cfg.HbmBinder] - <Mapping collection: model.Dept.students -> STUDENT>
    2006-07-15 08:54:33,906 INFO [org.hibernate.cfg.HbmBinder] - <Mapping collection: model.Singletype.singlethemes -> SINGLETHEME>
    2006-07-15 08:54:33,906 INFO [org.hibernate.cfg.Configuration] - <processing association property references>
    2006-07-15 08:54:33,906 INFO [org.hibernate.cfg.Configuration] - <processing foreign key constraints>
    2006-07-15 08:54:34,531 INFO [org.hibernate.connection.ConnectionProviderFactory] - <Initializing connection provider: org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider>
      

  4.   

    2006-07-15 08:54:37,375 INFO [org.hibernate.cfg.SettingsFactory] - <RDBMS: Oracle, version: Oracle9i Enterprise Edition Release 9.0.1.1.1 - Production
    With the Partitioning option
    JServer Release 9.0.1.1.1 - Production>
    2006-07-15 08:54:37,390 INFO [org.hibernate.cfg.SettingsFactory] - <JDBC driver: Oracle JDBC driver, version: 9.2.0.4.0>
    2006-07-15 08:54:37,500 INFO [org.hibernate.dialect.Dialect] - <Using dialect: org.hibernate.dialect.Oracle9Dialect>
    2006-07-15 08:54:37,546 INFO [org.hibernate.transaction.TransactionFactoryFactory] - <Using default transaction strategy (direct JDBC transactions)>
    2006-07-15 08:54:37,562 INFO [org.hibernate.transaction.TransactionManagerLookupFactory] - <No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)>
    2006-07-15 08:54:37,562 INFO [org.hibernate.cfg.SettingsFactory] - <Automatic flush during beforeCompletion(): disabled>
    2006-07-15 08:54:37,562 INFO [org.hibernate.cfg.SettingsFactory] - <Automatic session close at end of transaction: disabled>
    2006-07-15 08:54:37,562 INFO [org.hibernate.cfg.SettingsFactory] - <Scrollable result sets: enabled>
    2006-07-15 08:54:37,562 INFO [org.hibernate.cfg.SettingsFactory] - <JDBC3 getGeneratedKeys(): disabled>
    2006-07-15 08:54:37,562 INFO [org.hibernate.cfg.SettingsFactory] - <Connection release mode: on_close>
    2006-07-15 08:54:37,562 INFO [org.hibernate.cfg.SettingsFactory] - <Maximum outer join fetch depth: 10>
    2006-07-15 08:54:37,562 INFO [org.hibernate.cfg.SettingsFactory] - <Default batch fetch size: 1>
    2006-07-15 08:54:37,562 INFO [org.hibernate.cfg.SettingsFactory] - <Generate SQL with comments: disabled>
    2006-07-15 08:54:37,562 INFO [org.hibernate.cfg.SettingsFactory] - <Order SQL updates by primary key: disabled>
    2006-07-15 08:54:37,562 INFO [org.hibernate.cfg.SettingsFactory] - <Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory>
    2006-07-15 08:54:37,578 INFO [org.hibernate.hql.ast.ASTQueryTranslatorFactory] - <Using ASTQueryTranslatorFactory>
    2006-07-15 08:54:37,578 INFO [org.hibernate.cfg.SettingsFactory] - <Query language substitutions: {}>
    2006-07-15 08:54:37,578 INFO [org.hibernate.cfg.SettingsFactory] - <Second-level cache: enabled>
    2006-07-15 08:54:37,578 INFO [org.hibernate.cfg.SettingsFactory] - <Query cache: enabled>
    2006-07-15 08:54:37,578 INFO [org.hibernate.cfg.SettingsFactory] - <Cache provider: org.hibernate.cache.EhCacheProvider>
    2006-07-15 08:54:37,593 INFO [org.hibernate.cfg.SettingsFactory] - <Optimize cache for minimal puts: disabled>
    2006-07-15 08:54:37,593 INFO [org.hibernate.cfg.SettingsFactory] - <Structured second-level cache entries: disabled>
    2006-07-15 08:54:37,593 INFO [org.hibernate.cfg.SettingsFactory] - <Query cache factory: org.hibernate.cache.StandardQueryCacheFactory>
    2006-07-15 08:54:37,640 INFO [org.hibernate.cfg.SettingsFactory] - <Echoing all SQL to stdout>
    2006-07-15 08:54:37,640 INFO [org.hibernate.cfg.SettingsFactory] - <Statistics: disabled>
    2006-07-15 08:54:37,640 INFO [org.hibernate.cfg.SettingsFactory] - <Deleted entity synthetic identifier rollback: disabled>
    2006-07-15 08:54:37,640 INFO [org.hibernate.cfg.SettingsFactory] - <Default entity-mode: pojo>
    2006-07-15 08:54:37,734 INFO [org.hibernate.impl.SessionFactoryImpl] - <building session factory>
    2006-07-15 08:54:40,250 INFO [org.hibernate.impl.SessionFactoryObjectFactory] - <Not binding factory to JNDI, no JNDI name configured>
    2006-07-15 08:54:40,250 INFO [org.hibernate.cache.UpdateTimestampsCache] - <starting update timestamps cache at region: org.hibernate.cache.UpdateTimestampsCache>
    2006-07-15 08:54:40,250 WARN [org.hibernate.cache.EhCacheProvider] - <Could not find configuration [org.hibernate.cache.UpdateTimestampsCache]; using defaults.>
    2006-07-15 08:54:40,281 INFO [org.hibernate.cache.StandardQueryCache] - <starting query cache at region: org.hibernate.cache.StandardQueryCache>
    2006-07-15 08:54:40,281 WARN [org.hibernate.cache.EhCacheProvider] - <Could not find configuration [org.hibernate.cache.StandardQueryCache]; using defaults.>
    2006-07-15 08:54:40,281 INFO [org.hibernate.impl.SessionFactoryImpl] - <Checking 0 named queries>
      

  5.   

    2006-07-15 08:54:40,421 INFO [org.springframework.orm.hibernate3.HibernateTransactionManager] - <Using DataSource [org.springframework.jdbc.datasource.DriverManagerDataSource@109062e] of Hibernate SessionFactory for HibernateTransactionManager>
    2006-07-15 08:54:40,625 INFO [org.springframework.jdbc.datasource.JdbcTransactionObjectSupport] - <JDBC 3.0 Savepoint class is available>
    2006-07-15 08:54:40,921 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - <Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]>
    2006-07-15 08:54:40,968 INFO [org.springframework.jdbc.support.SQLErrorCodesFactory] - <SQLErrorCodes loaded: [DB2, HSQL, MS-SQL, MySQL, Oracle, Informix, PostgreSQL, Sybase]>
    Hibernate: select visitcount0_.ID as ID, visitcount0_.VISITCOUNT as VISITCOUNT1_, visitcount0_.VISITTIME as VISITTIME1_ from YJDBA.VISITCOUNT visitcount0_ where visitcount0_.VISITTIME>=? and visitcount0_.VISITTIME<=?
    Hibernate: insert into YJDBA.VISITCOUNT (VISITCOUNT, VISITTIME, ID) values (?, ?, ?)
    2006-07-15 08:54:42,031 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - <Loading XML bean definitions from class path resource [applicationContext.xml]>
    2006-07-15 08:54:42,062 INFO [org.springframework.jdbc.datasource.DriverManagerDataSource] - <Loaded JDBC driver: oracle.jdbc.driver.OracleDriver>
    2006-07-15 08:54:42,078 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Password -> PASSWORD>
    2006-07-15 08:54:42,093 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Visitcount -> VISITCOUNT>
    2006-07-15 08:54:42,109 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Infoissue -> INFOISSUE>
    2006-07-15 08:54:42,109 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Student -> STUDENT>
    2006-07-15 08:54:42,125 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Studentselectcourse -> STUDENTSELECTCOURSE>
    2006-07-15 08:54:42,140 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Course -> COURSE>
    2006-07-15 08:54:42,140 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Teacher -> TEACHER>
    2006-07-15 08:54:42,156 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Teachcourse -> TEACHCOURSE>
    2006-07-15 08:54:42,171 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Teststate -> TESTSTATE>
    2006-07-15 08:54:42,171 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Score -> SCORE>
    2006-07-15 08:54:42,187 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Major -> MAJOR>
    2006-07-15 08:54:42,203 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Dept -> DEPT>
    2006-07-15 08:54:42,203 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Userwords -> USERWORDS>
    2006-07-15 08:54:42,218 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Testpaper -> TESTPAPER>
    2006-07-15 08:54:42,218 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Singlepaper -> SINGLEPAPER>
    2006-07-15 08:54:42,250 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Singletheme -> SINGLETHEME>
    2006-07-15 08:54:42,265 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Singletype -> SINGLETYPE>
    2006-07-15 08:54:42,265 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Savesingle -> SAVESINGLE>
    2006-07-15 08:54:42,281 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Globevar -> GLOBEVAR>
    2006-07-15 08:54:42,296 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: model.Downloadissue -> DOWNLOADISSUE>
    2006-07-15 08:54:42,296 INFO [org.springframework.orm.hibernate3.LocalSessionFactoryBean] - <Building new Hibernate SessionFactory>
    2006-07-15 08:54:42,296 INFO [org.hibernate.cfg.Configuration] - <processing extends queue>
    2006-07-15 08:54:42,296 INFO [org.hibernate.cfg.Configuration] - <processing collection mappings>
    2006-07-15 08:54:42,296 INFO [org.hibernate.cfg.HbmBinder] - <Mapping collection: model.Major.students -> STUDENT>
    2006-07-15 08:54:42,296 INFO [org.hibernate.cfg.HbmBinder] - <Mapping collection: model.Dept.majors -> MAJOR>
    2006-07-15 08:54:42,296 INFO [org.hibernate.cfg.HbmBinder] - <Mapping collection: model.Dept.students -> STUDENT>
    2006-07-15 08:54:42,296 INFO [org.hibernate.cfg.HbmBinder] - <Mapping collection: model.Singletype.singlethemes -> SINGLETHEME>
    2006-07-15 08:54:42,296 INFO [org.hibernate.cfg.Configuration] - <processing association property references>
    2006-07-15 08:54:42,296 INFO [org.hibernate.cfg.Configuration] - <processing foreign key constraints>
    2006-07-15 08:54:42,296 INFO [org.hibernate.connection.ConnectionProviderFactory] - <Initializing connection provider: org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider>
    2006-07-15 08:54:42,390 INFO [org.hibernate.cfg.SettingsFactory] - <RDBMS: Oracle, version: Oracle9i Enterprise Edition Release 9.0.1.1.1 - Production
    With the Partitioning option
      

  6.   

    我觉得vlinux(神vlinux飘飘) 说的是对的,大家觉得呢,帮我看看上面我贴出的applicationcontext.xml和提示信息