Olá ,
não estou visualizando por que está fechando a sessão …
quando executo na sequência a busca ao DAO a primeira executa a segunda não…conforme abaixo.
alguém já passou por essa situação e poderia me ajudar …abs
codigo metodo chama DAO
// primeiroa busca no DAO busca o nome do instituto
InstitutoRN irn = new InstitutoRN();
Object[] in = irn.buscar();
if (in== null){
nome="Instituto não cadastrado";
}else{
@SuppressWarnings("unused")
BigInteger id= (BigInteger) in[0];
nome= (String) in[1];
}
// segunda busca no banco inicio busca qtde de aluno
try {
BigInteger in1 = irn.getCountAluno();
if (in1==null){
qtdeAluno = BigInteger.valueOf(0);
}else{
qtdeAluno= (BigInteger) in1;
}
} catch (Exception e) {
long valor =0;
qtdeAluno = BigInteger.valueOf(valor);
}
//fim busca qtde de aluno
System.out.println("qtdealuno="+qtdeAluno+"");
parametrosRelatorio.put("INSTITUTO", nome);
parametrosRelatorio.put("QTDEALUNO", qtdeAluno.toString());
DAO
public class InstitutoDAOHibernate implements InstitutoDAO {
private Session session;
public Session getSession() {
return session;
}
public void setSession(Session session) {
this.session = session;
}
public Object[] buscar() {
Object[] instituto = null;
String hql = "";
try {
this.session.beginTransaction().begin();
hql = "SELECT * FROM instituto LIMIT 1";
Query query = (Query) this.session.createSQLQuery(hql.toString());
instituto = (Object[]) query.uniqueResult();
this.session.beginTransaction().commit();
} catch (Exception e) {
System.out.println("Erro: " + e);
}
return instituto;
}
@Override
public BigInteger getCountAluno() {
BigInteger retorno = null;
String hql = "";
try {
this.session.beginTransaction().begin();
hql = "select count(distinct(nm_aluno)) from resposta_aluno";
Query query = (Query) this.session.createSQLQuery(hql);
retorno = (BigInteger) query.uniqueResult();
this.session.beginTransaction().commit();
} catch (Exception e) {
System.out.println("Erro " + e);
}
return retorno;
}
}
regra de negocio
import java.math.BigInteger;
import ao.com.avalia.instituto.util.DAOFactory;
public class InstitutoRN {
private InstitutoDAO institutoDAO;
public InstitutoRN() {
this.institutoDAO = DAOFactory.criarInstitutoDAO();
}
public Object[] buscar() {
return this.institutoDAO.buscar();
}
public BigInteger getCountAluno() {
return this.institutoDAO.getCountAluno();
}
}
fabrica
public class DAOFactory {
public static InstitutoDAO criarInstitutoDAO(){
InstitutoDAOHibernate institutoDAO = new InstitutoDAOHibernate();
institutoDAO.setSession(HibernateUtil.getSessionfactory().getCurrentSession());
return institutoDAO;
}
}
18/04/2011 08:15:08 org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jdk1.6.0_25\bin;.;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jdk1.6.0_25/jre/bin/server;C:/Program Files/Java/jdk1.6.0_25/jre/bin;C:/Program Files/Java/jdk1.6.0_25/jre/lib/amd64;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\WIDCOMM\Bluetooth Software\;C:\Program Files\WIDCOMM\Bluetooth Software\syswow64;C:\Program Files\Java\jdk1.6.0_25\bin;.;C:\Program Files\MySQL\MySQL Server 5.5\bin;C:\apache-maven-2.2.1;C:\Program Files (x86)\Windows Live\Shared;C:\apache-maven-2.2.1\bin;C:\eclipse_livro;
18/04/2011 08:15:08 org.apache.tomcat.util.digester.SetPropertiesRule begin
AVISO: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:rela' did not find a matching property.
18/04/2011 08:15:08 org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
18/04/2011 08:15:08 org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 907 ms
18/04/2011 08:15:09 org.apache.catalina.core.StandardService start
INFO: Starting service Catalina
18/04/2011 08:15:09 org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.26
18/04/2011 08:15:10 com.sun.faces.config.ConfigureListener contextInitialized
INFO: Inicializando Mojarra 2.0.3 (FCS b03) para o contexto '/rela'
18/04/2011 08:15:14 org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
18/04/2011 08:15:15 org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
18/04/2011 08:15:15 org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/187 config=null
18/04/2011 08:15:15 org.apache.catalina.startup.Catalina start
INFO: Server startup in 6125 ms
18/04/2011 08:15:29 com.sun.faces.renderkit.html_basic.HtmlBasicRenderer getForComponent
AVISO: Não foi possível encontrar o componente com a ID descricao na exibição.
234 [http-8080-2] INFO org.hibernate.annotations.common.Version - Hibernate Commons Annotations 3.2.0.Final
234 [http-8080-2] INFO org.hibernate.cfg.Environment - Hibernate 3.6.0.Final
250 [http-8080-2] INFO org.hibernate.cfg.Environment - hibernate.properties not found
250 [http-8080-2] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
250 [http-8080-2] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
438 [http-8080-2] INFO org.hibernate.cfg.Configuration - configuring from resource: hibernate.cfg.xml
438 [http-8080-2] INFO org.hibernate.cfg.Configuration - Configuration resource: hibernate.cfg.xml
516 [http-8080-2] WARN org.hibernate.util.DTDEntityResolver - recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
594 [http-8080-2] INFO org.hibernate.cfg.Configuration - Configured SessionFactory: null
687 [http-8080-2] INFO org.hibernate.cfg.AnnotationBinder - Binding entity from annotated class: ao.com.avalia.instituto.Instituto
828 [http-8080-2] INFO org.hibernate.cfg.annotations.EntityBinder - Bind entity ao.com.avalia.instituto.Instituto on table instituto
968 [http-8080-2] INFO org.hibernate.cfg.Configuration - Hibernate Validator not found: ignoring
984 [http-8080-2] INFO org.hibernate.cfg.search.HibernateSearchEventListenerRegister - Unable to find org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not enabled.
1015 [http-8080-2] INFO org.hibernate.connection.DriverManagerConnectionProvider - Using Hibernate built-in connection pool (not for production use!)
1015 [http-8080-2] INFO org.hibernate.connection.DriverManagerConnectionProvider - Hibernate connection pool size: 1
1046 [http-8080-2] INFO org.hibernate.connection.DriverManagerConnectionProvider - autocommit mode: false
1093 [http-8080-2] INFO org.hibernate.connection.DriverManagerConnectionProvider - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://127.0.0.1/avalia
1093 [http-8080-2] INFO org.hibernate.connection.DriverManagerConnectionProvider - connection properties: {user=root, password=****}
1421 [http-8080-2] INFO org.hibernate.cfg.SettingsFactory - Database ->
name : MySQL
version : 5.5.8
major : 5
minor : 5
1421 [http-8080-2] INFO org.hibernate.cfg.SettingsFactory - Driver ->
name : MySQL-AB JDBC Driver
version : mysql-connector-java-5.1.14 ( Revision: ${bzr.revision-id} )
major : 5
minor : 1
1483 [http-8080-2] INFO org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.MySQLDialect
1514 [http-8080-2] INFO org.hibernate.transaction.TransactionFactoryFactory - Using default transaction strategy (direct JDBC transactions)
1530 [http-8080-2] INFO org.hibernate.transaction.TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
1561 [http-8080-2] INFO org.hibernate.cfg.SettingsFactory - Automatic flush during beforeCompletion(): disabled
1561 [http-8080-2] INFO org.hibernate.cfg.SettingsFactory - Automatic session close at end of transaction: disabled
1561 [http-8080-2] INFO org.hibernate.cfg.SettingsFactory - JDBC batch size: 15
1561 [http-8080-2] INFO org.hibernate.cfg.SettingsFactory - JDBC batch updates for versioned data: disabled
1561 [http-8080-2] INFO org.hibernate.cfg.SettingsFactory - Scrollable result sets: enabled
1561 [http-8080-2] INFO org.hibernate.cfg.SettingsFactory - JDBC3 getGeneratedKeys(): enabled
1561 [http-8080-2] INFO org.hibernate.cfg.SettingsFactory - Connection release mode: auto
1561 [http-8080-2] INFO org.hibernate.cfg.SettingsFactory - Maximum outer join fetch depth: 2
1561 [http-8080-2] INFO org.hibernate.cfg.SettingsFactory - Default batch fetch size: 1
1561 [http-8080-2] INFO org.hibernate.cfg.SettingsFactory - Generate SQL with comments: disabled
1561 [http-8080-2] INFO org.hibernate.cfg.SettingsFactory - Order SQL updates by primary key: disabled
1561 [http-8080-2] INFO org.hibernate.cfg.SettingsFactory - Order SQL inserts for batching: disabled
1561 [http-8080-2] INFO org.hibernate.cfg.SettingsFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
1577 [http-8080-2] INFO org.hibernate.hql.ast.ASTQueryTranslatorFactory - Using ASTQueryTranslatorFactory
1577 [http-8080-2] INFO org.hibernate.cfg.SettingsFactory - Query language substitutions: {}
1577 [http-8080-2] INFO org.hibernate.cfg.SettingsFactory - JPA-QL strict compliance: disabled
1577 [http-8080-2] INFO org.hibernate.cfg.SettingsFactory - Second-level cache: enabled
1577 [http-8080-2] INFO org.hibernate.cfg.SettingsFactory - Query cache: disabled
1577 [http-8080-2] INFO org.hibernate.cfg.SettingsFactory - Cache region factory : org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge
1592 [http-8080-2] INFO org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge - Cache provider: org.hibernate.cache.NoCacheProvider
1592 [http-8080-2] INFO org.hibernate.cfg.SettingsFactory - Optimize cache for minimal puts: disabled
1592 [http-8080-2] INFO org.hibernate.cfg.SettingsFactory - Structured second-level cache entries: disabled
1608 [http-8080-2] INFO org.hibernate.cfg.SettingsFactory - Statistics: disabled
1608 [http-8080-2] INFO org.hibernate.cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled
1608 [http-8080-2] INFO org.hibernate.cfg.SettingsFactory - Default entity-mode: pojo
1608 [http-8080-2] INFO org.hibernate.cfg.SettingsFactory - Named query checking : enabled
1608 [http-8080-2] INFO org.hibernate.cfg.SettingsFactory - Check Nullability in Core (should be disabled when Bean Validation is on): enabled
1655 [http-8080-2] INFO org.hibernate.impl.SessionFactoryImpl - building session factory
2121 [http-8080-2] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
2137 [http-8080-2] INFO org.hibernate.tool.hbm2ddl.SchemaUpdate - Running hbm2ddl schema update
2137 [http-8080-2] INFO org.hibernate.tool.hbm2ddl.SchemaUpdate - fetching database metadata
2137 [http-8080-2] INFO org.hibernate.tool.hbm2ddl.SchemaUpdate - updating schema
2168 [http-8080-2] INFO org.hibernate.tool.hbm2ddl.TableMetadata - table found: avalia.instituto
2168 [http-8080-2] INFO org.hibernate.tool.hbm2ddl.TableMetadata - columns: [nm_instituto, id_instituto]
2168 [http-8080-2] INFO org.hibernate.tool.hbm2ddl.TableMetadata - foreign keys: []
2168 [http-8080-2] INFO org.hibernate.tool.hbm2ddl.TableMetadata - indexes: [primary]
2184 [http-8080-2] INFO org.hibernate.tool.hbm2ddl.SchemaUpdate - schema update complete
Erro org.hibernate.SessionException: Session is closed!
qtdealuno=0
hibernate
<?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>
<!--Database connection setting -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://127.0.0.1/avalia</property>
<property name="connection.username">root</property>
<property name="connection.password">sesese</property>
<!--JDBC connection pool(use built-in)-->
<property name="connection.pool_size">1</property>
<!--SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all execute SQL to stdout-->
<property name="show_sql">false</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="ao.com.avalia.instituto.Instituto"/>
</session-factory>
</hibernate-configuration>