我在springmvc中使用hib从数据库中获取数据时遇到以下异常。
SEVERE: Servlet.service() for servlet [spring] in context with path [/DemoApp] threw exception [Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection] with root cause
java.lang.NullPointerException: Cannot invoke "java.util.Map.get(Object)" because "this.serverVariables" is null
下面是我的代码块。
Session session = this.sessionFactory.getCurrentSession();
List<User> listUsr = session.createQuery("From User Where username=\'"+usr.getUsername()+"\' and passphare=\'"+usr.getPassphare()+"\'").list();
System.out.println("Lit user "+listUsr);
if(listUsr != null && listUsr.isEmpty()) {
System.out.println("Empty details");
} else {
System.out.println("Value fetched");
}
Sevlet代码..
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/mycart?useSSL=false" />
<property name="username" value="root" />
<property name="password" value="PASWORD" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.demoapp.model" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="connection.autoReconnect">true</prop>
<prop key="connection.autoReconnectForPools">true</prop>
<prop key="connection.is-connection-validation-required">true</prop>
<prop key="hibernate.c3p0.min_size">5</prop>
<prop key="hibernate.c3p0.max_size">20</prop>
<prop key="hibernate.c3p0.timeout">300</prop>
</props>
</property>
</bean>
<!-- Step 3: Setup Hibernate transaction manager -->
<bean id="myTransactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="myTransactionManager" />
<tx:annotation-driven/>
POM快照示例...
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.4.21.Final</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>5.3.6.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.21.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
3条答案
按热度按时间llmtgqce1#
这可能是驱动程序问题。请检查驱动程序版本和数据库版本。
3zwjbxry2#
我也遇到了同样的问题,无论研究和代码添加,我都无法解决这个问题。我建议使用https://start.spring.io/重新创建您的项目,添加Spring Web、Spring Data JPA和MySQL驱动程序依赖项。我也推荐这段视频,因为它走过了一个非常简单的连接到MySQL https://www.youtube.com/watch?v=e8g9eNnFpHQ。这对我确保我写的代码都不是问题很有帮助。我在你的代码中找不到任何明显的错误,但我希望这能提供一些解决方案。
eyh26e7m3#
原因:java.lang.空指针异常:在com.mysql.jdbc.ConnectionImpl.getServerCharset(连接实现Java:2997)https://github.com/spullara/mysql-connector-java/blob/master/src/main/java/com/mysql/jdbc/ConnectionImpl.java处为空
升级连接器,确保您有最新版本的mysql连接器