Spring Jdbc模板行映射器未在Object中设置所有值

时间:2017-02-09 22:05:43

标签: java spring oracle tomcat

我使用spring jdbc模板从oracle数据库中检索400条记录。 之后我使用行映射器将行转换为对象。对象中的某些值没有任何值。当我在pl / sql编辑器中测试相同的sql时,我能够看到行的所有行和所有列。当我调试应用程序时,我收到以下错误。如果我为同一个查询获取较少数量的记录(7),它工作正常。行映射器正在设置对象的所有值。你能帮我解决这个问题。

java.sql.SQLException: statement handle not executed

2017-02-09 15:45:39 INFO  XmlBeanDefinitionReader:317 - Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]
2017-02-09 15:45:39 INFO  SQLErrorCodesFactory:126 - SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase, Hana]
2017-02-09 15:45:39 WARN  SQLErrorCodesFactory:227 - Error while extracting database product name - falling back to empty error codes
org.springframework.jdbc.support.MetaDataAccessException: Error while extracting DatabaseMetaData; nested exception is java.sql.SQLException: Connection has already been closed.
    at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:305)
    at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:329)
    at org.springframework.jdbc.support.SQLErrorCodesFactory.getErrorCodes(SQLErrorCodesFactory.java:214)
    at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.setDataSource(SQLErrorCodeSQLExceptionTranslator.java:134)
    at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.<init>(SQLErrorCodeSQLExceptionTranslator.java:97)
    at org.springframework.jdbc.support.JdbcAccessor.getExceptionTranslator(JdbcAccessor.java:99)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:645)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:680)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:707)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:757)
    at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.query(NamedParameterJdbcTemplate.java:192)

1 个答案:

答案 0 :(得分:0)

如果从数据库中检索这400行需要一些时间,我假设您正在达到数据库连接超时。如果是,请在使用资源池时增加using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ShopEntity { class Product { public int ProductId { get; set; } public int Price { get; set; } public string ProductName { get; set; } } } using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace ShopEntity { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { using (var ctx = new ShopContext()) { User adminUser = new User() { Address = "Skejby Vænge 96A", EMailAddress = "anderskloborg@gmail.com", IsAdmin = 1, PassWord = "12345678", UserName = "Anders1234" }; //Product Apple = new Product() { Price = 2, ProductName = "Apple" }; //Product Melon = new Product() { Price = 10, ProductName = "Melon" }; //Product Beef = new Product() { Price = 40, ProductName = "Beef" }; ctx.Users.Add(adminUser); //ctx.Products.Add(Apple); //ctx.Products.Add(Melon); //ctx.Products.Add(Beef); ctx.SaveChanges(); } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } } using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ShopEntity { class ShopContext: DbContext { public ShopContext() : base() { } public DbSet<User> Users { get; set; } public DbSet<Product> Products { get; set; } } } using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ShopEntity { class User { public int UserId { get; set; } [MaxLength(20)] public string UserName { get; set; } [MaxLength(20)] public string PassWord { get; set; } public string EMailAddress { get; set; } [MaxLength(50)] public string Address { get; set; } public byte IsAdmin { get; set; } } } <?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> </startup> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> </providers> </entityFramework> </configuration> 。调试代码时也可能发生超时。

也可能是数据库端的超时。在这种情况下,您必须调整数据库超时参数。